n8n-nodes-graphiti-memory
Version:
Community node for self-hosted Graphiti (Zep) memory
175 lines (174 loc) • 6.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphitiMemory = void 0;
class GraphitiMemory {
constructor() {
this.description = {
displayName: 'Graphiti Memory',
name: 'graphitiMemory',
group: ['transform'],
version: 1,
description: 'Lê e grava memória em uma instância Graphiti self-host',
defaults: {
name: 'Graphiti Memory'
},
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
credentials: [
{
name: 'graphitiApi',
required: true
}
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Get Memory',
value: 'get',
description: 'Buscar mensagens relevantes'
},
{
name: 'Add Messages',
value: 'add',
description: 'Gravar mensagem do usuário + IA'
},
],
default: 'get',
noDataExpression: true,
},
{
displayName: 'Group ID',
name: 'groupId',
type: 'string',
default: 'default',
required: true,
description: 'ID do grupo para organizar as memórias',
},
// Campos para GET
{
displayName: 'Query',
name: 'query',
type: 'string',
displayOptions: {
show: {
operation: ['get']
}
},
default: '',
required: true,
description: 'Consulta para buscar memórias relevantes',
},
// Campos para ADD
{
displayName: 'User Message',
name: 'userMsg',
type: 'string',
displayOptions: {
show: {
operation: ['add']
}
},
default: '',
required: true,
description: 'Mensagem do usuário',
},
{
displayName: 'Assistant Message',
name: 'assistantMsg',
type: 'string',
displayOptions: {
show: {
operation: ['add']
}
},
default: '',
required: true,
description: 'Resposta do assistente',
},
],
};
}
async execute() {
const returnItems = [];
for (let i = 0; i < this.getInputData().length; i++) {
try {
const credentials = await this.getCredentials('graphitiApi');
if (!credentials) {
throw new Error('No credentials found');
}
// Preparar base URL
let baseUrl = credentials.baseUrl.replace(/\/+$/, '');
if (credentials.useHttps === false) {
baseUrl = baseUrl.replace(/^https:/, 'http:');
}
// Preparar headers
const headers = {
'Content-Type': 'application/json'
};
if (credentials.apiKey) {
headers.Authorization = `Bearer ${credentials.apiKey}`;
}
const operation = this.getNodeParameter('operation', i);
const groupId = this.getNodeParameter('groupId', i);
if (operation === 'get') {
const query = this.getNodeParameter('query', i);
const response = await this.helpers.httpRequest({
url: `${baseUrl}/get-memory`,
method: 'POST',
body: {
group_id: groupId,
query
},
headers,
json: true,
});
returnItems.push({
json: response,
pairedItem: { item: i }
});
}
else if (operation === 'add') {
const userMsg = this.getNodeParameter('userMsg', i);
const assistantMsg = this.getNodeParameter('assistantMsg', i);
const response = await this.helpers.httpRequest({
url: `${baseUrl}/messages`,
method: 'POST',
body: {
group_id: groupId,
messages: [
{ role: 'user', content: userMsg },
{ role: 'assistant', content: assistantMsg },
],
},
headers,
json: true,
});
returnItems.push({
json: response,
pairedItem: { item: i }
});
}
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (this.continueOnFail()) {
returnItems.push({
json: {
error: errorMessage
},
pairedItem: { item: i }
});
}
else {
throw error;
}
}
}
return [returnItems];
}
}
exports.GraphitiMemory = GraphitiMemory;