UNPKG

@gotohuman/n8n-nodes-gotohuman

Version:

n8n node to request human reviews in AI workflows with gotoHuman

187 lines 7.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.gotoHumanWebhookMethods = exports.BASE_URL = void 0; exports.getTriggerOn = getTriggerOn; exports.isAgentWebhookTrigger = isAgentWebhookTrigger; exports.getWebhookFormId = getWebhookFormId; exports.getWebhookAgentId = getWebhookAgentId; exports.fetchN8nFormsListSearch = fetchN8nFormsListSearch; exports.fetchN8nAgentsListSearch = fetchN8nAgentsListSearch; const n8n_workflow_1 = require("n8n-workflow"); exports.BASE_URL = 'https://api.gotohuman.com'; async function gotoHumanHookRequest(hook, options) { var _a; const requestOptions = { ...options, returnFullResponse: true, ignoreHttpStatusErrors: true, }; let responseData; try { responseData = await hook.helpers.httpRequestWithAuthentication.call(hook, 'gotoHumanApi', requestOptions); } catch (error) { throw new n8n_workflow_1.NodeApiError(hook.getNode(), error); } const statusCode = (responseData === null || responseData === void 0 ? void 0 : responseData.statusCode) || 200; if (String(statusCode).startsWith('4') || String(statusCode).startsWith('5')) { const body = responseData.body; let message; if (typeof body === 'object' && body !== null && typeof body.error === 'string') { message = body.error; } else if (typeof body === 'string') { message = body; } else if (body !== undefined) { message = JSON.stringify(body); } else { message = JSON.stringify(responseData); } throw new n8n_workflow_1.NodeApiError(hook.getNode(), responseData, { message, httpCode: String(statusCode), }); } return ((_a = responseData.body) !== null && _a !== void 0 ? _a : responseData); } function getTriggerOn(hook) { return hook.getNodeParameter('triggerOn') || 'onReviewCompletion'; } function isAgentWebhookTrigger(hook) { return getTriggerOn(hook) === 'onAgentMessageOrToolResult'; } function getWebhookFormId(hook) { const triggerOn = getTriggerOn(hook); const paramName = triggerOn === 'onTriggerFormSubmission' ? 'triggerFormID' : 'reviewTemplateID'; const formObj = hook.getNodeParameter(paramName); return formObj.value; } function getWebhookAgentId(hook) { const agentObj = hook.getNodeParameter('agentID'); return agentObj.value; } async function fetchN8nFormsListSearch(filter, query) { const queryString = (query === null || query === void 0 ? void 0 : query.onlyTriggerForms) ? '?onlyTriggerForms=true' : ''; const options = { method: 'GET', url: `${exports.BASE_URL}/fetchN8nForms${queryString}`, json: true, }; const response = await this.helpers.httpRequestWithAuthentication.call(this, 'gotoHumanApi', options); if (response === undefined) { const emptyLabel = (query === null || query === void 0 ? void 0 : query.onlyTriggerForms) ? 'No trigger forms found. Please create one first in our web app.' : 'No review templates found. Please create one first in our web app.'; throw new n8n_workflow_1.NodeOperationError(this.getNode(), emptyLabel); } return { results: ((response === null || response === void 0 ? void 0 : response.forms) || []) .filter((form) => !filter || form.label.toLowerCase().includes(filter.toLowerCase())) .map((form) => ({ name: form.label, value: form.value, })), }; } async function fetchN8nAgentsListSearch(filter) { const options = { method: 'GET', url: `${exports.BASE_URL}/fetchN8nAgents`, json: true, }; const response = await this.helpers.httpRequestWithAuthentication.call(this, 'gotoHumanApi', options); if (response === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No agents found. Please create one first in our web app.'); } return { results: ((response === null || response === void 0 ? void 0 : response.agents) || []) .filter((agent) => !filter || agent.label.toLowerCase().includes(filter.toLowerCase())) .map((agent) => ({ name: agent.label, value: agent.value, })), }; } exports.gotoHumanWebhookMethods = { default: { async checkExists() { const webhookUrl = this.getNodeWebhookUrl('default'); if (isAgentWebhookTrigger(this)) { const agentId = getWebhookAgentId(this); const response = await gotoHumanHookRequest(this, { method: 'POST', url: `${exports.BASE_URL}/checkAgentWebhook`, body: { agentId, webhookUrl }, json: true, }); return response.exists === true; } const formId = getWebhookFormId(this); const response = await gotoHumanHookRequest(this, { method: 'POST', url: `${exports.BASE_URL}/checkWebhook`, body: { formId, webhookUrl }, json: true, }); return response.exists === true; }, async create() { const webhookUrl = this.getNodeWebhookUrl('default'); if (isAgentWebhookTrigger(this)) { const agentId = getWebhookAgentId(this); const response = await gotoHumanHookRequest(this, { method: 'POST', url: `${exports.BASE_URL}/createAgentWebhook`, body: { agentId, webhookUrl }, json: true, }); if (response.success) { const webhookData = this.getWorkflowStaticData('node'); webhookData.webhookId = response.webhookId; return true; } return false; } const formId = getWebhookFormId(this); const response = await gotoHumanHookRequest(this, { method: 'POST', url: `${exports.BASE_URL}/createWebhook`, body: { formId, webhookUrl }, json: true, }); if (response.success) { const webhookData = this.getWorkflowStaticData('node'); webhookData.webhookId = response.webhookId; return true; } return false; }, async delete() { const webhookUrl = this.getNodeWebhookUrl('default'); if (isAgentWebhookTrigger(this)) { const agentId = getWebhookAgentId(this); await gotoHumanHookRequest(this, { method: 'POST', url: `${exports.BASE_URL}/deleteAgentWebhook`, body: { agentId, webhookUrl }, json: true, }); } else { const formId = getWebhookFormId(this); await gotoHumanHookRequest(this, { method: 'POST', url: `${exports.BASE_URL}/deleteWebhook`, body: { formId, webhookUrl }, json: true, }); } const webhookData = this.getWorkflowStaticData('node'); delete webhookData.webhookId; return true; }, }, }; //# sourceMappingURL=helpers.js.map