UNPKG

n8n-nodes-agenthub

Version:

n8n nodes for AgentHub integration

699 lines (698 loc) 28.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgentHub = void 0; const axios_1 = __importDefault(require("axios")); class AgentHub { constructor() { this.description = { displayName: 'AgentHub', name: 'agentHub', icon: 'file:agenthub.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Interact with AgentHub AI agents', defaults: { name: 'AgentHub', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'agentHubApi', required: true, }, ], requestDefaults: { baseURL: '={{$credentials?.agentHubApi?.baseUrl}}', headers: { 'X-API-Key': '={{$credentials?.agentHubApi?.apiKey}}', 'Content-Type': 'application/json', }, }, properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Agent', value: 'agent', }, { name: 'Hiring', value: 'hiring', }, { name: 'Execution', value: 'execution', }, { name: 'Deployment', value: 'deployment', }, ], default: 'agent', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: [ 'agent', ], }, }, options: [ { name: 'List Agents', value: 'list', description: 'List available agents', action: 'List available agents', }, { name: 'Get Agent', value: 'get', description: 'Get agent details by ID', action: 'Get agent details by ID', }, { name: 'Search Agents', value: 'search', description: 'Search agents by query', action: 'Search agents by query', }, ], default: 'list', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: [ 'hiring', ], }, }, options: [ { name: 'Hire Agent', value: 'hire', description: 'Hire an agent for use', action: 'Hire an agent for use', }, { name: 'List Hirings', value: 'list', description: 'List your hired agents', action: 'List your hired agents', }, { name: 'Get Hiring Details', value: 'get', description: 'Get hiring details by ID', action: 'Get hiring details by ID', }, { name: 'Cancel Hiring', value: 'cancel', description: 'Cancel an active hiring', action: 'Cancel an active hiring', }, { name: 'Suspend Hiring', value: 'suspend', description: 'Suspend an active hiring', action: 'Suspend an active hiring', }, { name: 'Activate Hiring', value: 'activate', description: 'Activate a suspended hiring', action: 'Activate a suspended hiring', }, ], default: 'hire', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: [ 'execution', ], }, }, options: [ { name: 'Execute Agent', value: 'execute', description: 'Execute a hired agent', action: 'Execute a hired agent', }, { name: 'Get Execution Status', value: 'getStatus', description: 'Get execution status by ID', action: 'Get execution status by ID', }, { name: 'Initialize Agent', value: 'initialize', description: 'Initialize a persistent agent', action: 'Initialize a persistent agent', }, ], default: 'execute', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: [ 'deployment', ], }, }, options: [ { name: 'Create Deployment', value: 'create', description: 'Create deployment for a hiring', action: 'Create deployment for a hiring', }, { name: 'Get Deployment Status', value: 'getStatus', description: 'Get deployment status by ID', action: 'Get deployment status by ID', }, { name: 'Stop Deployment', value: 'stop', description: 'Stop a running deployment', action: 'Stop a running deployment', }, { name: 'List Deployments', value: 'list', description: 'List your deployments', action: 'List your deployments', }, ], default: 'create', }, // Agent Operations { displayName: 'Agent ID', name: 'agentId', type: 'string', displayOptions: { show: { resource: ['agent'], operation: ['get'], }, }, default: '', required: true, description: 'The ID of the agent to retrieve', }, { displayName: 'Query', name: 'query', type: 'string', displayOptions: { show: { resource: ['agent'], operation: ['search'], }, }, default: '', description: 'Search query for agents', }, { displayName: 'Category', name: 'category', type: 'string', displayOptions: { show: { resource: ['agent'], operation: ['search', 'list'], }, }, default: '', description: 'Filter by agent category', }, { displayName: 'Limit', name: 'limit', type: 'number', displayOptions: { show: { resource: ['agent'], operation: ['search', 'list'], }, }, default: 10, description: 'Maximum number of results to return', }, // Hiring Operations { displayName: 'Agent ID', name: 'hiringAgentId', type: 'string', displayOptions: { show: { resource: ['hiring'], operation: ['hire'], }, }, default: '', required: true, description: 'The ID of the agent to hire', }, { displayName: 'Requirements', name: 'requirements', type: 'json', displayOptions: { show: { resource: ['hiring'], operation: ['hire'], }, }, default: '{}', description: 'JSON configuration for the agent', }, { displayName: 'Billing Cycle', name: 'billingCycle', type: 'options', displayOptions: { show: { resource: ['hiring'], operation: ['hire'], }, }, options: [ { name: 'Per Use', value: 'per_use', }, { name: 'Monthly', value: 'monthly', }, ], default: 'per_use', description: 'Billing cycle for the hiring', }, { displayName: 'Hiring ID', name: 'hiringId', type: 'number', displayOptions: { show: { resource: ['hiring'], operation: ['get', 'cancel', 'suspend', 'activate'], }, }, default: 0, required: true, description: 'The ID of the hiring', }, { displayName: 'Notes', name: 'notes', type: 'string', displayOptions: { show: { resource: ['hiring'], operation: ['cancel', 'suspend', 'activate'], }, }, default: '', description: 'Notes for the operation', }, // Execution Operations { displayName: 'Hiring ID', name: 'executionHiringId', type: 'number', displayOptions: { show: { resource: ['execution'], operation: ['execute', 'initialize'], }, }, default: 0, required: true, description: 'The ID of the hiring to execute', }, { displayName: 'Input Data', name: 'inputData', type: 'json', displayOptions: { show: { resource: ['execution'], operation: ['execute'], }, }, default: '{}', required: true, description: 'JSON input data for the agent execution', }, { displayName: 'Wait for Completion', name: 'waitForCompletion', type: 'boolean', displayOptions: { show: { resource: ['execution'], operation: ['execute'], }, }, default: true, description: 'Whether to wait for execution completion', }, { displayName: 'Timeout (seconds)', name: 'timeout', type: 'number', displayOptions: { show: { resource: ['execution'], operation: ['execute'], }, }, default: 60, description: 'Timeout in seconds when waiting for completion', }, { displayName: 'Initialization Config', name: 'initConfig', type: 'json', displayOptions: { show: { resource: ['execution'], operation: ['initialize'], }, }, default: '{}', required: true, description: 'JSON configuration for agent initialization', }, { displayName: 'Execution ID', name: 'executionId', type: 'string', displayOptions: { show: { resource: ['execution'], operation: ['getStatus'], }, }, default: '', required: true, description: 'The ID of the execution to check', }, // Deployment Operations { displayName: 'Hiring ID', name: 'deploymentHiringId', type: 'number', displayOptions: { show: { resource: ['deployment'], operation: ['create'], }, }, default: 0, required: true, description: 'The ID of the hiring to deploy', }, { displayName: 'Deployment ID', name: 'deploymentId', type: 'string', displayOptions: { show: { resource: ['deployment'], operation: ['getStatus', 'stop'], }, }, default: '', required: true, description: 'The ID of the deployment', }, { displayName: 'Status Filter', name: 'deploymentStatus', type: 'options', displayOptions: { show: { resource: ['deployment'], operation: ['list'], }, }, options: [ { name: 'Running', value: 'running', }, { name: 'Building', value: 'building', }, { name: 'Deploying', value: 'deploying', }, { name: 'Pending', value: 'pending', }, { name: 'Stopped', value: 'stopped', }, { name: 'Failed', value: 'failed', }, { name: 'Crashed', value: 'crashed', }, { name: 'All', value: 'all', }, ], default: 'running', description: 'Filter deployments by status', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('agentHubApi'); let endpoint; let method; let body = {}; let qs = {}; for (let i = 0; i < items.length; i++) { try { if (resource === 'agent') { if (operation === 'list') { method = 'GET'; endpoint = '/api/v1/marketplace/search'; qs = { limit: this.getNodeParameter('limit', i), category: this.getNodeParameter('category', i) || undefined, }; } else if (operation === 'get') { method = 'GET'; const agentId = this.getNodeParameter('agentId', i); endpoint = `/api/v1/agents/${agentId}`; } else if (operation === 'search') { method = 'GET'; endpoint = '/api/v1/marketplace/search'; qs = { query: this.getNodeParameter('query', i), limit: this.getNodeParameter('limit', i), category: this.getNodeParameter('category', i) || undefined, }; } } else if (resource === 'hiring') { if (operation === 'hire') { method = 'POST'; endpoint = '/api/v1/hiring/'; body = { agent_id: this.getNodeParameter('hiringAgentId', i), user_id: credentials.userId || 1, requirements: JSON.parse(this.getNodeParameter('requirements', i)), billing_cycle: this.getNodeParameter('billingCycle', i), }; } else if (operation === 'list') { method = 'GET'; endpoint = '/api/v1/hiring/'; qs = { user_id: credentials.userId || 1, }; } else if (operation === 'get') { method = 'GET'; const hiringId = this.getNodeParameter('hiringId', i); endpoint = `/api/v1/hiring/${hiringId}`; } else if (operation === 'cancel') { method = 'POST'; const hiringId = this.getNodeParameter('hiringId', i); endpoint = `/api/v1/hiring/${hiringId}/cancel`; body = { notes: this.getNodeParameter('notes', i) || '', }; } else if (operation === 'suspend') { method = 'POST'; const hiringId = this.getNodeParameter('hiringId', i); endpoint = `/api/v1/hiring/${hiringId}/suspend`; body = { notes: this.getNodeParameter('notes', i) || '', }; } else if (operation === 'activate') { method = 'POST'; const hiringId = this.getNodeParameter('hiringId', i); endpoint = `/api/v1/hiring/${hiringId}/activate`; body = { notes: this.getNodeParameter('notes', i) || '', }; } } else if (resource === 'execution') { if (operation === 'execute') { method = 'POST'; const hiringId = this.getNodeParameter('executionHiringId', i); const waitForCompletion = this.getNodeParameter('waitForCompletion', i); if (waitForCompletion) { endpoint = `/api/v1/execution/`; body = { hiring_id: hiringId, execution_type: 'run', input_data: JSON.parse(this.getNodeParameter('inputData', i)), user_id: credentials.userId || 1, }; } else { endpoint = `/api/v1/execution/`; body = { hiring_id: hiringId, execution_type: 'run', input_data: JSON.parse(this.getNodeParameter('inputData', i)), user_id: credentials.userId || 1, }; } } else if (operation === 'initialize') { method = 'POST'; const hiringId = this.getNodeParameter('executionHiringId', i); endpoint = `/api/v1/execution/`; body = { hiring_id: hiringId, execution_type: 'initialize', input_data: JSON.parse(this.getNodeParameter('initConfig', i)), user_id: credentials.userId || 1, }; } else if (operation === 'getStatus') { method = 'GET'; const executionId = this.getNodeParameter('executionId', i); endpoint = `/api/v1/execution/${executionId}`; } } else if (resource === 'deployment') { if (operation === 'create') { method = 'POST'; const hiringId = this.getNodeParameter('deploymentHiringId', i); endpoint = `/api/v1/deployment/`; body = { hiring_id: hiringId, }; } else if (operation === 'getStatus') { method = 'GET'; const deploymentId = this.getNodeParameter('deploymentId', i); endpoint = `/api/v1/deployment/${deploymentId}/status`; } else if (operation === 'stop') { method = 'POST'; const deploymentId = this.getNodeParameter('deploymentId', i); endpoint = `/api/v1/deployment/${deploymentId}/stop`; } else if (operation === 'list') { method = 'GET'; endpoint = '/api/v1/deployment/'; qs = { status: this.getNodeParameter('deploymentStatus', i), }; } } // Remove undefined query parameters Object.keys(qs).forEach(key => { if (qs[key] === undefined) { delete qs[key]; } }); const requestConfig = { method, url: endpoint, params: Object.keys(qs).length ? qs : undefined, data: Object.keys(body).length ? body : undefined, headers: { 'X-API-Key': credentials.apiKey, 'Content-Type': 'application/json', }, }; const response = await (0, axios_1.default)(requestConfig); returnData.push(response.data); } catch (error) { if (this.continueOnFail()) { returnData.push({ error: error.message }); continue; } throw error; } } return [this.helpers.returnJsonArray(returnData)]; } } exports.AgentHub = AgentHub;