n8n-nodes-recallio
Version:
RecallioAI Memory node for n8n
151 lines • 7.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecallioMemory = exports.recallioMemoryToMessages = exports.condenseRecallioMemoryIntoHumanMessage = exports.recallioMemoryContextToSystemPrompt = void 0;
const RecallioClient_1 = require("./RecallioClient");
const memory_1 = require("@langchain/core/memory");
const messages_1 = require("@langchain/core/messages");
const chat_memory_1 = require("@langchain/community/memory/chat_memory");
const recallioMemoryContextToSystemPrompt = (memories, summary) => {
const parts = [];
if (summary === null || summary === void 0 ? void 0 : summary.content)
parts.push(String(summary.content));
if (Array.isArray(memories) && memories.length > 0) {
const content = memories
.map((m) => m === null || m === void 0 ? void 0 : m.content)
.filter((c) => Boolean(c))
.join("\n");
if (content)
parts.push(content);
}
return parts.join("\n");
};
exports.recallioMemoryContextToSystemPrompt = recallioMemoryContextToSystemPrompt;
const condenseRecallioMemoryIntoHumanMessage = (memories, summary) => {
const basePrompt = "These are the memories I have stored. Give more weight to the user's current question. Adjust your answer based on the memories if relevant; otherwise, you may ignore them. Do not explicitly reference this memory section in your reply. The USER MEMORIES are:\n\n";
const systemPrompt = (0, exports.recallioMemoryContextToSystemPrompt)(memories, summary);
return new messages_1.HumanMessage(`${basePrompt}\n${systemPrompt}`);
};
exports.condenseRecallioMemoryIntoHumanMessage = condenseRecallioMemoryIntoHumanMessage;
const recallioMemoryToMessages = (memories, summary) => {
const systemPrompt = (0, exports.recallioMemoryContextToSystemPrompt)(memories, summary);
return systemPrompt ? [new messages_1.SystemMessage(systemPrompt)] : [];
};
exports.recallioMemoryToMessages = recallioMemoryToMessages;
class RecallioMemory extends chat_memory_1.BaseChatMemory {
constructor(fields, httpRequest) {
var _a, _b, _c, _d, _e, _f, _g, _h;
super({
returnMessages: (_a = fields === null || fields === void 0 ? void 0 : fields.returnMessages) !== null && _a !== void 0 ? _a : false,
inputKey: fields === null || fields === void 0 ? void 0 : fields.inputKey,
outputKey: fields === null || fields === void 0 ? void 0 : fields.outputKey,
});
this.humanPrefix = "Human";
this.aiPrefix = "AI";
this.memoryKey = "history";
if (!fields.apiKey)
throw new Error("apiKey is required for RecallioMemory");
if (!fields.sessionId)
throw new Error("sessionId is required for RecallioMemory");
if (!fields.projectId)
throw new Error("projectId is required for RecallioMemory");
this.humanPrefix = (_b = fields.humanPrefix) !== null && _b !== void 0 ? _b : this.humanPrefix;
this.aiPrefix = (_c = fields.aiPrefix) !== null && _c !== void 0 ? _c : this.aiPrefix;
this.memoryKey = (_d = fields.memoryKey) !== null && _d !== void 0 ? _d : this.memoryKey;
this.apiKey = fields.apiKey;
this.projectId = fields.projectId;
this.sessionId = fields.sessionId;
this.scope = (_e = fields.scope) !== null && _e !== void 0 ? _e : "user";
this.tags = fields.tags;
this.recallOptions = fields.recallOptions;
this.consentFlag = (_f = fields.consentFlag) !== null && _f !== void 0 ? _f : true;
this.separateMessages = (_g = fields.separateMessages) !== null && _g !== void 0 ? _g : false;
const clientOpts = {
apiKey: this.apiKey,
...((_h = fields.clientOptions) !== null && _h !== void 0 ? _h : {}),
};
this.client = new RecallioClient_1.RecallioClient(clientOpts, httpRequest);
}
get memoryKeys() {
return [this.memoryKey];
}
async loadMemoryVariables(values) {
var _a, _b;
const hasQuery = Boolean(values.input);
let memories = null;
let summary = null;
try {
if (hasQuery) {
memories = await this.client.recallMemory({
userId: this.sessionId,
projectId: this.projectId,
query: String((_a = values.input) !== null && _a !== void 0 ? _a : "*"),
scope: this.scope,
tags: this.tags,
}, this.recallOptions);
}
else {
summary = await this.client.recallSummary({
userId: this.sessionId,
projectId: this.projectId,
scope: this.scope,
tags: this.tags,
});
}
}
catch (error) {
console.error("RecallIO: failed to load memories:", error);
return this.returnMessages
? { [this.memoryKey]: [] }
: { [this.memoryKey]: "" };
}
if (this.returnMessages) {
return {
[this.memoryKey]: this.separateMessages
? (0, exports.recallioMemoryToMessages)(memories, summary)
: [(0, exports.condenseRecallioMemoryIntoHumanMessage)(memories, summary)],
};
}
return {
[this.memoryKey]: this.separateMessages
? (0, messages_1.getBufferString)((0, exports.recallioMemoryToMessages)(memories, summary), this.humanPrefix, this.aiPrefix)
: (_b = (0, exports.condenseRecallioMemoryIntoHumanMessage)(memories, summary).content) !== null && _b !== void 0 ? _b : "",
};
}
async saveContext(inputValues, outputValues) {
const input = (0, memory_1.getInputValue)(inputValues, this.inputKey);
const output = (0, memory_1.getOutputValue)(outputValues, this.outputKey);
if (!input || !output) {
console.warn("RecallIO: missing input or output; skipping memory write");
return;
}
const content = `${this.humanPrefix}: ${String(input)}\n${this.aiPrefix}: ${String(output)}`;
try {
await this.client.writeMemory({
userId: this.sessionId,
projectId: this.projectId,
content,
tags: this.tags,
consentFlag: this.consentFlag,
});
}
catch (error) {
console.error("RecallIO: failed to write memory:", error);
}
await super.saveContext(inputValues, outputValues);
}
async clear() {
try {
await this.client.deleteMemory({
scope: this.scope,
userId: this.sessionId,
projectId: this.projectId,
});
}
catch (error) {
console.error("RecallIO: failed to clear memories:", error);
}
await super.clear();
}
}
exports.RecallioMemory = RecallioMemory;
//# sourceMappingURL=RecallioCommunity.js.map