UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

142 lines 5.23 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var memoryManagement_exports = {}; __export(memoryManagement_exports, { buildMessagesFromSteps: () => buildMessagesFromSteps, buildToolContext: () => buildToolContext, extractToolCallId: () => extractToolCallId, loadMemory: () => loadMemory, saveToMemory: () => saveToMemory }); module.exports = __toCommonJS(memoryManagement_exports); var import_messages = require("@langchain/core/messages"); function extractToolCallId(toolCallId, toolName) { if (typeof toolCallId === "string" && toolCallId.length > 0) { return toolCallId; } if (typeof toolCallId === "object" && toolCallId !== null && !Array.isArray(toolCallId) && "id" in toolCallId) { const id = toolCallId.id; if (typeof id === "string" && id.length > 0) { return id; } } if (Array.isArray(toolCallId) && toolCallId.length > 0) { return extractToolCallId(toolCallId[0], toolName); } return `synthetic_${toolName}_${Date.now()}`; } function buildMessagesFromSteps(steps) { const messages = []; for (let i = 0; i < steps.length; i++) { const step = steps[i]; const existingAIMessage = step.action.messageLog?.[0]; const existingToolCallId = existingAIMessage?.tool_calls?.[0]?.id; const toolCallId = existingToolCallId ?? extractToolCallId(step.action.toolCallId, step.action.tool); const aiMessage = existingAIMessage ?? new import_messages.AIMessage({ content: `Calling ${step.action.tool} with input: ${JSON.stringify(step.action.toolInput)}`, tool_calls: [ { id: toolCallId, name: step.action.tool, args: step.action.toolInput, type: "tool_call" } ] }); const toolMessage = new import_messages.ToolMessage({ content: step.observation, tool_call_id: toolCallId, name: step.action.tool }); messages.push(aiMessage); messages.push(toolMessage); } return messages; } function buildToolContext(steps) { return steps.map( (step) => `Tool: ${step.action.tool}, Input: ${JSON.stringify(step.action.toolInput)}, Result: ${step.observation}` ).join("; "); } function cleanupOrphanedMessages(chatHistory) { while (chatHistory.length > 0 && chatHistory[0] instanceof import_messages.ToolMessage) { chatHistory.shift(); } const firstMessage = chatHistory[0]; const hasOrphanedAIMessage = firstMessage instanceof import_messages.AIMessage && (firstMessage.tool_calls?.length ?? 0) > 0 && !(chatHistory[1] instanceof import_messages.ToolMessage); if (hasOrphanedAIMessage) { chatHistory.shift(); } return chatHistory; } async function loadMemory(memory, model, maxTokens) { if (!memory) { return void 0; } const memoryVariables = await memory.loadMemoryVariables({}); let chatHistory = memoryVariables["chat_history"] || []; chatHistory = cleanupOrphanedMessages(chatHistory); if (maxTokens && model) { chatHistory = await (0, import_messages.trimMessages)(chatHistory, { strategy: "last", maxTokens, tokenCounter: model, includeSystem: true, startOn: "human", allowPartial: true }); chatHistory = cleanupOrphanedMessages(chatHistory); } return chatHistory; } async function saveToMemory(input, output, memory, steps, previousStepsCount) { if (!output || !memory) { return; } if (!steps || steps.length === 0) { await memory.saveContext({ input }, { output }); return; } const newSteps = previousStepsCount ? steps.slice(previousStepsCount) : steps; if (newSteps.length === 0) { await memory.saveContext({ input }, { output }); return; } if (!("addMessages" in memory.chatHistory) || typeof memory.chatHistory.addMessages !== "function") { const toolContext = buildToolContext(newSteps); const fullOutput = `[Used tools: ${toolContext}] ${output}`; await memory.saveContext({ input }, { output: fullOutput }); return; } const messages = []; messages.push(new import_messages.HumanMessage(input)); const toolMessages = buildMessagesFromSteps(newSteps); messages.push.apply(messages, toolMessages); messages.push(new import_messages.AIMessage(output)); await memory.chatHistory.addMessages(messages); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildMessagesFromSteps, buildToolContext, extractToolCallId, loadMemory, saveToMemory }); //# sourceMappingURL=memoryManagement.js.map