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

@@ -3,7 +3,7 @@ import type { EventNamesAiNodesType, IDataObject, IExecuteFunctions } from 'n8n-
import { BaseChatModel } from 'langchain/chat_models/base';
import { BaseChatModel as BaseChatModelCore } from '@langchain/core/language_models/chat_models';
import type { BaseOutputParser } from '@langchain/core/output_parsers';
import { BaseMessage } from 'langchain/schema';
import type { BaseMessage } from 'langchain/schema';
export function getMetadataFiltersValues(
ctx: IExecuteFunctions,
@@ -67,6 +67,39 @@ export function getPromptInputByType(options: {
return input;
}
export function getSessionId(
ctx: IExecuteFunctions,
itemIndex: number,
selectorKey = 'sessionIdType',
autoSelect = 'fromInput',
customKey = 'sessionKey',
) {
let sessionId = '';
const selectorType = ctx.getNodeParameter(selectorKey, itemIndex) as string;
if (selectorType === autoSelect) {
sessionId = ctx.evaluateExpression('{{ $json.sessionId }}', itemIndex) as string;
if (sessionId === '' || sessionId === undefined) {
throw new NodeOperationError(ctx.getNode(), 'No session ID found', {
description:
"Expected to find the session ID in an input field called 'sessionId' (this is what the chat trigger node outputs). To use something else, change the 'Session ID' parameter",
itemIndex,
});
}
} else {
sessionId = ctx.getNodeParameter(customKey, itemIndex, '') as string;
if (sessionId === '' || sessionId === undefined) {
throw new NodeOperationError(ctx.getNode(), 'Key parameter is empty', {
description:
"Provide a key to use as session ID in the 'Key' parameter or use the 'Take from previous node automatically' option to use the session ID from the previous node, e.t. chat trigger node",
itemIndex,
});
}
}
return sessionId;
}
export async function logAiEvent(
executeFunctions: IExecuteFunctions,
event: EventNamesAiNodesType,
@@ -79,7 +112,7 @@ export async function logAiEvent(
}
}
export function serializeChatHistory (chatHistory: Array<BaseMessage>): string {
export function serializeChatHistory(chatHistory: BaseMessage[]): string {
return chatHistory
.map((chatMessage) => {
if (chatMessage._getType() === 'human') {