n8n-nodes-base
Version:
Base nodes of n8n
146 lines • 5.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.description = void 0;
exports.execute = execute;
const agent_utils_1 = require("./agent.utils");
const constants_1 = require("../../constants");
const transport_1 = require("../../transport");
const displayOptions = {
show: {
resource: ['agent'],
operation: ['run'],
},
};
exports.description = [
{
displayName: 'Agent',
name: 'agentId',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
required: true,
description: 'The Airtop agent to run. Visit <a href="https://portal.airtop.ai/agents" target="_blank">Airtop Agents</a> for more information.',
displayOptions: {
show: {
resource: ['agent'],
operation: ['run'],
},
},
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select an Agent...',
typeOptions: {
searchListMethod: 'listSearchAgents',
searchFilterRequired: false,
searchable: true,
},
},
{
displayName: 'By ID',
name: 'id',
type: 'string',
placeholder: 'e.g. agent_abc123',
validation: [
{
type: 'regex',
properties: {
regex: '.+',
errorMessage: 'Agent ID cannot be empty',
},
},
],
},
],
},
{
displayName: 'Agent Parameters',
name: 'agentParameters',
type: 'resourceMapper',
noDataExpression: true,
default: {
mappingMode: 'defineBelow',
value: null,
},
typeOptions: {
loadOptionsDependsOn: ['agentId.value'],
resourceMapper: {
resourceMapperMethod: 'agentsResourceMapping',
mode: 'map',
supportAutoMap: false,
addAllFields: true,
noFieldsError: 'No input parameters found for the selected agent',
multiKeyMatch: false,
allowEmptyValues: true,
fieldWords: {
singular: 'parameter',
plural: 'parameters',
},
},
},
displayOptions: {
show: {
resource: ['agent'],
operation: ['run'],
},
hide: {
agentId: [''],
},
},
},
{
displayName: 'Await Agent',
name: 'awaitExecution',
type: 'boolean',
default: true,
description: 'Whether to wait for the agent to complete its execution',
displayOptions,
},
{
displayName: 'Timeout',
name: 'timeout',
type: 'number',
default: 600,
description: 'Timeout in seconds to wait for the agent to finish',
displayOptions: {
show: {
resource: ['agent'],
operation: ['run'],
awaitExecution: [true],
},
},
},
];
async function execute(index) {
const airtopNode = this.getNode();
const agentId = this.getNodeParameter('agentId', index, '', {
extractValue: true,
});
const agentParameters = this.getNodeParameter('agentParameters', index, {});
const awaitExecution = this.getNodeParameter('awaitExecution', index, true);
const timeout = this.getNodeParameter('timeout', index, 600);
// Validate timeout
(0, agent_utils_1.throwOperationErrorIf)(timeout < constants_1.AGENT_MIN_TIMEOUT_SECONDS, constants_1.ERROR_MESSAGES.AGENT_TIMEOUT_INVALID, airtopNode);
// Convert fixedCollection parameters to API format
const validatedAgentParameters = agent_utils_1.validateAgentParameters.call(this, agentParameters);
const { webhookId } = await agent_utils_1.getAgentDetails.call(this, agentId);
const invokeUrl = `${constants_1.AIRTOP_HOOKS_BASE_URL}/agents/${agentId}/webhooks/${webhookId}`;
const invocationResponse = await transport_1.apiRequest.call(this, 'POST', invokeUrl, validatedAgentParameters);
const invocationId = invocationResponse.invocationId;
(0, agent_utils_1.throwOperationErrorIf)(!invocationId, "No 'invocationId' received from agent webhook response", airtopNode);
if (!awaitExecution) {
return this.helpers.returnJsonArray({
invocationId,
});
}
// Poll for agent's execution status
const result = await agent_utils_1.pollAgentStatus.call(this, agentId, invocationId, timeout);
(0, agent_utils_1.throwOperationErrorIf)(Boolean(result?.error), `${result?.error ?? 'Unknown error'}. Agent Invocation ID: ${invocationId}`, airtopNode);
return this.helpers.returnJsonArray({
invocationId,
status: result?.status ?? 'Unknown',
output: result?.output ?? {},
});
}
//# sourceMappingURL=run.operation.js.map