@kaia-team/n8n-nodes-kaia
Version:
n8n nodes for Kaia LLM integration
460 lines • 18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KaiaTrigger = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class KaiaTrigger {
constructor() {
this.description = {
displayName: 'Kaia Trigger',
name: 'kaiaTrigger',
icon: 'file:kaia.svg',
group: ['trigger'],
version: 1,
description: 'Trigger workflows on Kaia system webhook events',
defaults: {
name: 'Kaia Trigger',
},
inputs: [],
outputs: ["main" /* NodeConnectionType.Main */],
credentials: [
{
name: 'kaiaApi',
required: true,
},
],
codex: {
categories: ['AI & ML'],
subcategories: {
'AI & ML': ['LLM', 'Chatbot'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.kaia.team',
},
],
},
},
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'kaia-webhook',
},
],
properties: [
{
displayName: 'Trigger Type',
name: 'triggerType',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Webhook Event',
value: 'webhookEvent',
description: 'Trigger on webhook events from Kaia',
},
{
name: 'Create System Webhook',
value: 'createWebhook',
description: 'Create a system webhook in Kaia',
},
],
default: 'webhookEvent',
},
// Webhook creation options
{
displayName: 'Webhook Name',
name: 'webhookName',
type: 'string',
default: '',
placeholder: 'My n8n Webhook',
displayOptions: {
show: {
triggerType: ['createWebhook'],
},
},
description: 'Name for the system webhook',
required: true,
},
{
displayName: 'Models to Listen For',
name: 'models',
type: 'multiOptions',
displayOptions: {
show: {
triggerType: ['createWebhook'],
},
},
options: [
{
name: 'ApiConfig',
value: 'ApiConfig',
description: 'API configuration changes',
},
{
name: 'Assistant',
value: 'Assistant',
description: 'Assistant configuration changes',
},
{
name: 'ChatHistory',
value: 'ChatHistory',
description: 'Chat history updates',
},
{
name: 'Document',
value: 'Document',
description: 'Document changes',
},
{
name: 'SystemWebhook',
value: 'SystemWebhook',
description: 'Webhook management events',
},
{
name: 'UserAccount',
value: 'UserAccount',
description: 'User account changes',
},
{
name: 'VecDb',
value: 'VecDb',
description: 'Vector database changes',
},
],
default: [],
description: 'Models to listen for events on',
required: true,
},
{
displayName: 'Event Types',
name: 'eventTypes',
type: 'multiOptions',
displayOptions: {
show: {
triggerType: ['createWebhook'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'When a new record is created',
},
{
name: 'Delete',
value: 'delete',
description: 'When a record is deleted',
},
{
name: 'Update',
value: 'update',
description: 'When a record is updated',
},
],
default: ['create', 'update'],
description: 'Types of events to listen for',
required: true,
},
{
displayName: 'N8N Webhook Base URL',
name: 'n8nWebhookBaseUrl',
type: 'string',
default: '',
displayOptions: {
show: {
triggerType: ['createWebhook'],
},
},
placeholder: 'https://your-n8n-instance.com/webhook',
description: 'Base URL for n8n webhooks (used to generate webhook URLs)',
required: true,
},
{
displayName: 'Filter by Model',
name: 'filterModel',
type: 'options',
displayOptions: {
show: {
triggerType: ['webhookEvent'],
},
},
options: [
{
name: 'All Models',
value: 'all',
},
{
name: 'ApiConfig',
value: 'ApiConfig',
},
{
name: 'Assistant',
value: 'Assistant',
},
{
name: 'ChatHistory',
value: 'ChatHistory',
},
{
name: 'Document',
value: 'Document',
},
{
name: 'SystemWebhook',
value: 'SystemWebhook',
},
{
name: 'UserAccount',
value: 'UserAccount',
},
{
name: 'VecDb',
value: 'VecDb',
},
],
default: 'all',
description: 'Filter webhook events by model type',
},
{
displayName: 'Filter by Event Type',
name: 'filterEventType',
type: 'options',
displayOptions: {
show: {
triggerType: ['webhookEvent'],
},
},
options: [
{
name: 'All Events',
value: 'all',
},
{
name: 'Create',
value: 'create',
},
{
name: 'Delete',
value: 'delete',
},
{
name: 'Update',
value: 'update',
},
],
default: 'all',
description: 'Filter webhook events by event type',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
triggerType: ['createWebhook'],
},
},
options: [
{
displayName: 'Description',
name: 'description',
type: 'string',
default: '',
description: 'Description for the webhook',
},
{
displayName: 'Active',
name: 'active',
type: 'boolean',
default: true,
description: 'Whether the webhook should be active',
},
],
},
{
displayName: 'Setup Instructions',
name: 'setupInstructions',
type: 'notice',
default: '',
displayOptions: {
show: {
triggerType: ['webhookEvent'],
},
},
description: 'To setup the webhook in your Kaia instance: 1. Go to admin panel. 2. Navigate to System Webhooks. 3. Create new with URL from n8n webhook, select models/events. 4. Copy ID and configure in credentials if needed.',
},
],
};
}
async trigger() {
const triggerType = this.getNodeParameter('triggerType');
const staticData = this.getWorkflowStaticData('node');
if (triggerType === 'createWebhook') {
const setup = await createSystemWebhook(this);
staticData.webhookId = setup.webhookId;
return {
closeFunction: async () => {
await onWebhookDelete(this);
},
manualTriggerFunction: async () => { },
};
}
else {
const setup = await setupWebhookTrigger(this);
const filterModel = this.getNodeParameter('filterModel');
const filterEventType = this.getNodeParameter('filterEventType');
staticData.filterModel = filterModel;
staticData.filterEventType = filterEventType;
return {
closeFunction: async () => { },
manualTriggerFunction: async () => { },
};
}
}
// @ts-ignore
async webhook() {
var _a, _b, _c, _d, _e, _f;
const body = this.getBodyData();
const staticData = this.getWorkflowStaticData('node');
const filterModel = staticData.filterModel;
const filterEventType = staticData.filterEventType;
// Validate webhook payload
if (!body || !body.event_type || !body.model) {
return { noData: true };
}
// Apply filters
if (filterModel !== 'all' && body.model !== filterModel) {
return { noData: true };
}
if (filterEventType !== 'all' && body.event_type !== filterEventType) {
return { noData: true };
}
// Process the webhook data
const webhookData = {
event_type: body.event_type,
model: body.model,
changes: body.changes || {},
object: body.object || {},
timestamp: new Date().toISOString(),
};
// Add model-specific processing
if (body.model === 'UserAccount') {
webhookData.user_id = (_a = body.object) === null || _a === void 0 ? void 0 : _a.id;
webhookData.user_email = (_b = body.object) === null || _b === void 0 ? void 0 : _b.email;
}
else if (body.model === 'ChatHistory') {
webhookData.chat_id = (_c = body.object) === null || _c === void 0 ? void 0 : _c.id;
webhookData.user_id = (_d = body.object) === null || _d === void 0 ? void 0 : _d.user_id;
}
else if (body.model === 'Assistant') {
webhookData.assistant_id = (_e = body.object) === null || _e === void 0 ? void 0 : _e.id;
webhookData.assistant_name = (_f = body.object) === null || _f === void 0 ? void 0 : _f.title;
}
return {
workflowData: [this.helpers.returnJsonArray([webhookData])],
};
}
async onWebhookDelete() {
const credentials = await this.getCredentials('kaiaApi');
const adminUrl = credentials.adminUrl;
const adminToken = credentials.adminToken;
const staticData = this.getWorkflowStaticData('node');
const webhookId = staticData.webhookId;
if (webhookId) {
try {
await this.helpers.httpRequest({
method: 'DELETE',
url: `${adminUrl}/system_webhooks/${webhookId}`,
headers: {
'X-User-Token': adminToken,
},
});
}
catch (error) {
// Log error but don't throw - webhook might already be deleted
console.error('Failed to delete system webhook:', error.message);
}
}
}
}
exports.KaiaTrigger = KaiaTrigger;
// Move private methods to standalone functions
async function createSystemWebhook(context) {
var _a, _b;
const credentials = await context.getCredentials('kaiaApi');
const adminUrl = credentials.adminUrl;
const adminToken = credentials.adminToken;
const n8nWebhookBaseUrl = context.getNodeParameter('n8nWebhookBaseUrl');
const webhookName = context.getNodeParameter('webhookName');
const models = context.getNodeParameter('models');
const eventTypes = context.getNodeParameter('eventTypes');
const additionalFields = context.getNodeParameter('additionalFields');
const webhookUrl = `${n8nWebhookBaseUrl}/${context.getNode().id}`;
const payload = {
system_webhook: {
name: webhookName,
url: webhookUrl,
models,
description: additionalFields.description,
active: additionalFields.active !== false,
},
};
try {
const response = await context.helpers.httpRequest({
method: 'POST',
url: `${adminUrl}/system_webhooks`,
body: payload,
headers: {
'Content-Type': 'application/json',
'X-User-Token': adminToken,
},
});
const webhookId = response.id;
const staticData = context.getWorkflowStaticData('node');
staticData.webhookId = webhookId;
return {
webhookId,
webhookUrl,
responseData: response,
};
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to create system webhook: ${((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message}`);
}
}
async function setupWebhookTrigger(context) {
const filterModel = context.getNodeParameter('filterModel');
const filterEventType = context.getNodeParameter('filterEventType');
// @ts-ignore
context.getNode().context.set('filterModel', filterModel);
// @ts-ignore
context.getNode().context.set('filterEventType', filterEventType);
return {};
}
async function onWebhookDelete(context) {
const credentials = await context.getCredentials('kaiaApi');
const adminUrl = credentials.adminUrl;
const adminToken = credentials.adminToken;
const staticData = context.getWorkflowStaticData('node');
const webhookId = staticData.webhookId;
if (webhookId) {
try {
await context.helpers.httpRequest({
method: 'DELETE',
url: `${adminUrl}/system_webhooks/${webhookId}`,
headers: {
'X-User-Token': adminToken,
},
});
}
catch (error) {
console.error('Failed to delete system webhook:', error.message);
}
}
}
//# sourceMappingURL=KaiaTrigger.node.js.map