feat: Session is selector for memory nodes (#8736)

This commit is contained in:
Michael Kret
2024-02-27 15:01:15 +02:00
committed by GitHub
parent 5f6da7b84e
commit 2aaf211dfc
7 changed files with 188 additions and 22 deletions

View File

@@ -6,13 +6,16 @@ import { BufferMemory } from 'langchain/memory';
import { BaseClient } from '@xata.io/client';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
import { sessionIdOption, sessionKeyProperty } from '../descriptions';
import { getSessionId } from '../../../utils/helpers';
export class MemoryXata implements INodeType {
description: INodeTypeDescription = {
displayName: 'Xata',
name: 'memoryXata',
icon: 'file:xata.svg',
group: ['transform'],
version: [1, 1.1],
version: [1, 1.1, 1.2],
description: 'Use Xata Memory',
defaults: {
name: 'Xata',
@@ -69,11 +72,29 @@ export class MemoryXata implements INodeType {
},
},
},
{
...sessionIdOption,
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
},
sessionKeyProperty,
],
};
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('xataApi');
const nodeVersion = this.getNode().typeVersion;
let sessionId;
if (nodeVersion >= 1.2) {
sessionId = getSessionId(this, itemIndex);
} else {
sessionId = this.getNodeParameter('sessionId', itemIndex) as string;
}
const xataClient = new BaseClient({
apiKey: credentials.apiKey as string,
@@ -81,8 +102,6 @@ export class MemoryXata implements INodeType {
databaseURL: credentials.databaseEndpoint as string,
});
const sessionId = this.getNodeParameter('sessionId', itemIndex) as string;
const table = (credentials.databaseEndpoint as string).match(
/https:\/\/[^.]+\.[^.]+\.xata\.sh\/db\/([^\/:]+)/,
);
@@ -94,18 +113,21 @@ export class MemoryXata implements INodeType {
);
}
const chatHistory = new XataChatMessageHistory({
table: table[1],
sessionId,
client: xataClient,
apiKey: credentials.apiKey as string,
});
const memory = new BufferMemory({
chatHistory: new XataChatMessageHistory({
table: table[1],
sessionId,
client: xataClient,
apiKey: credentials.apiKey as string,
}),
chatHistory,
memoryKey: 'chat_history',
returnMessages: true,
inputKey: 'input',
outputKey: 'output',
});
return {
response: logWrapper(memory, this),
};