UNPKG

n8n-nodes-gigachat

Version:

A user-friendly GigaChat AI (Sber) nodes for n8n

71 lines 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertMessagesGigachat = convertMessagesGigachat; async function convertMessagesGigachat(ctx, i, memory) { var _a, _b, _c, _d; const aiOptions = ctx.getNodeParameter('options', i); const items = ctx.getInputData(); const sessionId = ((_b = (_a = items[0]) === null || _a === void 0 ? void 0 : _a.json) === null || _b === void 0 ? void 0 : _b.sessionId) || ((_d = (_c = items[0]) === null || _c === void 0 ? void 0 : _c.json) === null || _d === void 0 ? void 0 : _d.chat_session_id) || ctx.getNodeParameter('sessionId', 0, ''); if (!sessionId) { console.warn('We have not provided Session ID, please provide it in input instead'); } const messages = []; if (aiOptions.systemMessage) { messages.push({ role: 'system', content: aiOptions.systemMessage, }); } if (memory) { const memoryVariables = await memory.loadMemoryVariables({}); const chatHistory = memoryVariables['chat_history'] || []; for (const msg of chatHistory) { const msgType = msg.getType(); if (msgType === 'human') { messages.push({ role: 'user', content: msg.content.toString(), }); } else if (msgType === 'ai') { const content = msg.content.toString(); const toolCallMatch = content.match(/\[Used tools: (.*?)\] (.*)/s); if (toolCallMatch) { const toolsText = toolCallMatch[1]; const actualResponse = toolCallMatch[2]; const toolRegex = /Tool: ([^,]+), Input: (.*?), Result: (.*?)(?:;|$)/g; let match; while ((match = toolRegex.exec(toolsText)) !== null) { const [_, toolName, toolInput, toolResult] = match; messages.push({ role: 'assistant', function_call: { name: toolName.trim(), arguments: JSON.parse(toolInput.trim()), }, }); messages.push({ role: 'function', name: toolName.trim(), content: toolResult.trim(), }); } messages.push({ role: 'assistant', content: actualResponse, }); } else { messages.push({ role: 'assistant', content: content, }); } } } } return { sessionId: String(sessionId), messages }; } //# sourceMappingURL=Memory.js.map