n8n-nodes-zep-stemx-memory
Version:
n8n community node for Zep AI Memory Service
707 lines (706 loc) • 30.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZepMemory = void 0;
class ZepMemory {
constructor() {
this.description = {
displayName: 'Zep Memory',
name: 'zepMemory',
icon: 'file:zep.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Interact with Zep AI Memory Service',
defaults: {
name: 'Zep Memory',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'zepStemxApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Collection',
value: 'collection',
},
{
name: 'Document',
value: 'document',
},
{
name: 'Memory',
value: 'memory',
},
{
name: 'Session',
value: 'session',
},
{
name: 'User',
value: 'user',
},
],
default: 'memory',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['user'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new user',
action: 'Create a user',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a user',
action: 'Delete a user',
},
{
name: 'Get',
value: 'get',
description: 'Get a user',
action: 'Get a user',
},
{
name: 'List',
value: 'list',
description: 'List users',
action: 'List users',
},
{
name: 'Update',
value: 'update',
description: 'Update a user',
action: 'Update a user',
},
],
default: 'create',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['session'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new session',
action: 'Create a session',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a session',
action: 'Delete a session',
},
{
name: 'Get',
value: 'get',
description: 'Get a session',
action: 'Get a session',
},
{
name: 'Get User',
value: 'getUser',
description: 'Get user data from session',
action: 'Get session user',
},
{
name: 'List',
value: 'list',
description: 'List sessions',
action: 'List sessions',
},
{
name: 'Update',
value: 'update',
description: 'Update a session',
action: 'Update a session',
},
],
default: 'create',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['memory'],
},
},
options: [
{
name: 'Add',
value: 'add',
description: 'Add messages to memory',
action: 'Add memory',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete session memory',
action: 'Delete memory',
},
{
name: 'Get',
value: 'get',
description: 'Get session memory',
action: 'Get memory',
},
],
default: 'get',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['collection'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new collection',
action: 'Create a collection',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a collection',
action: 'Delete a collection',
},
{
name: 'Get',
value: 'get',
description: 'Get a collection',
action: 'Get a collection',
},
{
name: 'List',
value: 'list',
description: 'List collections',
action: 'List collections',
},
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['document'],
},
},
options: [
{
name: 'Add',
value: 'add',
description: 'Add a document to collection',
action: 'Add document',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a document',
action: 'Delete document',
},
{
name: 'Get',
value: 'get',
description: 'Get a document',
action: 'Get document',
},
{
name: 'List',
value: 'list',
description: 'List documents in collection',
action: 'List documents',
},
],
default: 'list',
},
// User fields
{
displayName: 'User ID',
name: 'userId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'get', 'update', 'delete'],
},
},
default: '',
description: 'The unique identifier for the user',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
placeholder: 'name@email.com',
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'update'],
},
},
default: '',
description: 'User email address',
},
{
displayName: 'First Name',
name: 'firstName',
type: 'string',
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'update'],
},
},
default: '',
description: 'User first name',
},
{
displayName: 'Last Name',
name: 'lastName',
type: 'string',
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'update'],
},
},
default: '',
description: 'User last name',
},
{
displayName: 'Partner ID',
name: 'partnerId',
type: 'string',
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'update'],
},
},
default: '',
description: 'Partner ID to be stored in metadata.partnerid',
},
{
displayName: 'User ID Meta',
name: 'userIdMeta',
type: 'string',
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'update'],
},
},
default: '',
description: 'User ID metadata to be stored in metadata.userid',
},
{
displayName: 'Fact Rating Instruction',
name: 'factRatingInstruction',
type: 'json',
displayOptions: {
show: {
resource: ['user'],
operation: ['create', 'update'],
},
},
default: '{}',
description: 'Instructions for fact rating (e.g., {"high": "Very important", "medium": "Somewhat important", "low": "Less important"})',
},
// Session fields
{
displayName: 'Session ID',
name: 'sessionId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['session', 'memory'],
},
},
default: '',
description: 'The unique identifier for the session',
},
{
displayName: 'User ID',
name: 'userId',
type: 'string',
displayOptions: {
show: {
resource: ['session'],
operation: ['create', 'update'],
},
},
default: '',
description: 'The user ID to associate with the session',
},
// Memory fields
{
displayName: 'Messages',
name: 'messages',
type: 'json',
required: true,
displayOptions: {
show: {
resource: ['memory'],
operation: ['add'],
},
},
default: '[{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}]',
description: 'Array of message objects with role and content',
},
{
displayName: 'Metadata',
name: 'metadata',
type: 'json',
displayOptions: {
show: {
resource: ['user', 'session', 'collection', 'document'],
operation: ['create', 'update', 'add'],
},
},
default: '{}',
description: 'Additional metadata as JSON object',
},
// Collection fields
{
displayName: 'Collection Name',
name: 'collectionName',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['collection'],
operation: ['create', 'get', 'delete'],
},
},
default: '',
description: 'The name of the collection',
},
{
displayName: 'Collection Name',
name: 'collectionName',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['document'],
},
},
default: '',
description: 'The name of the collection',
},
{
displayName: 'Description',
name: 'description',
type: 'string',
displayOptions: {
show: {
resource: ['collection'],
operation: ['create'],
},
},
default: '',
description: 'Collection description',
},
// Document fields
{
displayName: 'Document ID',
name: 'documentId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['document'],
operation: ['add', 'get', 'delete'],
},
},
default: '',
description: 'The unique identifier for the document',
},
{
displayName: 'Content',
name: 'content',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['document'],
operation: ['add'],
},
},
default: '',
description: 'The document content',
},
],
};
}
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('zepStemxApi');
const baseUrl = credentials.baseUrl;
const apiKey = credentials.apiKey;
for (let i = 0; i < items.length; i++) {
try {
const options = {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
returnFullResponse: false,
url: '',
method: 'GET',
};
if (resource === 'user') {
if (operation === 'list') {
options.url = `${baseUrl}/users`;
options.method = 'GET';
}
else if (operation === 'create') {
const userId = this.getNodeParameter('userId', i);
const email = this.getNodeParameter('email', i);
const firstName = this.getNodeParameter('firstName', i);
const lastName = this.getNodeParameter('lastName', i);
const metadata = this.getNodeParameter('metadata', i);
const partnerId = this.getNodeParameter('partnerId', i);
const userIdMeta = this.getNodeParameter('userIdMeta', i);
const factRatingInstruction = this.getNodeParameter('factRatingInstruction', i);
// Build metadata object
const metadataObj = metadata ? JSON.parse(metadata) : {};
if (partnerId) {
metadataObj.partnerid = partnerId;
}
if (userIdMeta) {
metadataObj.userid = userIdMeta;
}
options.url = `${baseUrl}/users`;
options.method = 'POST';
options.body = {
user_id: userId,
email: email || undefined,
first_name: firstName || undefined,
last_name: lastName || undefined,
metadata: Object.keys(metadataObj).length > 0 ? metadataObj : undefined,
fact_rating_instruction: factRatingInstruction ? JSON.parse(factRatingInstruction) : undefined,
};
}
else if (operation === 'get') {
const userId = this.getNodeParameter('userId', i);
options.url = `${baseUrl}/users/${userId}`;
options.method = 'GET';
}
else if (operation === 'update') {
const userId = this.getNodeParameter('userId', i);
const email = this.getNodeParameter('email', i);
const firstName = this.getNodeParameter('firstName', i);
const lastName = this.getNodeParameter('lastName', i);
const metadata = this.getNodeParameter('metadata', i);
const partnerId = this.getNodeParameter('partnerId', i);
const userIdMeta = this.getNodeParameter('userIdMeta', i);
options.url = `${baseUrl}/users/${userId}`;
options.method = 'PUT';
options.body = {
user_id: userId,
email: email || undefined,
first_name: firstName || undefined,
last_name: lastName || undefined,
metadata: metadata ? JSON.parse(metadata) : undefined,
partner_id: partnerId || undefined,
user_id_meta: userIdMeta || undefined,
};
}
else if (operation === 'delete') {
const userId = this.getNodeParameter('userId', i);
options.url = `${baseUrl}/users/${userId}`;
options.method = 'DELETE';
}
}
else if (resource === 'session') {
if (operation === 'list') {
options.url = `${baseUrl}/sessions`;
options.method = 'GET';
}
else if (operation === 'create') {
const sessionId = this.getNodeParameter('sessionId', i);
const userId = this.getNodeParameter('userId', i);
const metadata = this.getNodeParameter('metadata', i);
options.url = `${baseUrl}/sessions`;
options.method = 'POST';
options.body = {
session_id: sessionId,
user_id: userId || undefined,
metadata: metadata ? JSON.parse(metadata) : undefined,
};
}
else if (operation === 'get') {
const sessionId = this.getNodeParameter('sessionId', i);
options.url = `${baseUrl}/sessions/${sessionId}`;
options.method = 'GET';
}
else if (operation === 'getUser') {
const sessionId = this.getNodeParameter('sessionId', i);
options.url = `${baseUrl}/sessions/${sessionId}/user`;
options.method = 'GET';
}
else if (operation === 'update') {
const sessionId = this.getNodeParameter('sessionId', i);
const userId = this.getNodeParameter('userId', i);
const metadata = this.getNodeParameter('metadata', i);
options.url = `${baseUrl}/sessions/${sessionId}`;
options.method = 'PUT';
options.body = {
session_id: sessionId,
user_id: userId || undefined,
metadata: metadata ? JSON.parse(metadata) : undefined,
};
}
else if (operation === 'delete') {
const sessionId = this.getNodeParameter('sessionId', i);
options.url = `${baseUrl}/sessions/${sessionId}`;
options.method = 'DELETE';
}
}
else if (resource === 'memory') {
if (operation === 'get') {
const sessionId = this.getNodeParameter('sessionId', i);
options.url = `${baseUrl}/memory/${sessionId}`;
options.method = 'GET';
}
else if (operation === 'add') {
const sessionId = this.getNodeParameter('sessionId', i);
const messages = this.getNodeParameter('messages', i);
options.url = `${baseUrl}/memory`;
options.method = 'POST';
options.body = {
session_id: sessionId,
messages: JSON.parse(messages),
};
}
else if (operation === 'delete') {
const sessionId = this.getNodeParameter('sessionId', i);
options.url = `${baseUrl}/memory/${sessionId}`;
options.method = 'DELETE';
}
}
else if (resource === 'collection') {
if (operation === 'list') {
options.url = `${baseUrl}/collections`;
options.method = 'GET';
}
else if (operation === 'create') {
const collectionName = this.getNodeParameter('collectionName', i);
const description = this.getNodeParameter('description', i);
const metadata = this.getNodeParameter('metadata', i);
options.url = `${baseUrl}/collections`;
options.method = 'POST';
options.body = {
name: collectionName,
description: description || undefined,
metadata: metadata ? JSON.parse(metadata) : undefined,
};
}
else if (operation === 'get') {
const collectionName = this.getNodeParameter('collectionName', i);
options.url = `${baseUrl}/collections/${collectionName}`;
options.method = 'GET';
}
else if (operation === 'delete') {
const collectionName = this.getNodeParameter('collectionName', i);
options.url = `${baseUrl}/collections/${collectionName}`;
options.method = 'DELETE';
}
}
else if (resource === 'document') {
if (operation === 'list') {
const collectionName = this.getNodeParameter('collectionName', i);
options.url = `${baseUrl}/documents/${collectionName}`;
options.method = 'GET';
}
else if (operation === 'add') {
const collectionName = this.getNodeParameter('collectionName', i);
const documentId = this.getNodeParameter('documentId', i);
const content = this.getNodeParameter('content', i);
const metadata = this.getNodeParameter('metadata', i);
options.url = `${baseUrl}/documents`;
options.method = 'POST';
options.body = {
collection_name: collectionName,
document_id: documentId,
content: content,
metadata: metadata ? JSON.parse(metadata) : undefined,
};
}
else if (operation === 'get') {
const collectionName = this.getNodeParameter('collectionName', i);
const documentId = this.getNodeParameter('documentId', i);
options.url = `${baseUrl}/documents/${collectionName}/${documentId}`;
options.method = 'GET';
}
else if (operation === 'delete') {
const collectionName = this.getNodeParameter('collectionName', i);
const documentId = this.getNodeParameter('documentId', i);
options.url = `${baseUrl}/documents/${collectionName}/${documentId}`;
options.method = 'DELETE';
}
}
const responseData = await this.helpers.request(options);
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } });
returnData.push(...executionData);
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.ZepMemory = ZepMemory;