UNPKG

@n8n/n8n-nodes-langchain

Version:
87 lines • 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageFormatter = void 0; const tool_error_result_1 = require("../../../../utils/tool-error-result"); const utils_1 = require("../../shared/utils"); function parseMcpContentArrayString(value) { try { const parsed = JSON.parse(value); return (0, utils_1.isMcpContentArray)(parsed) ? parsed : null; } catch { return null; } } class MessageFormatter { static formatToolResult(result, isError = false, isFromMcpTool = false) { let content; if (isFromMcpTool && (0, utils_1.isMcpContentArray)(result)) { content = result; } else if (typeof result === 'object' && result !== null) { content = [{ type: 'text', text: JSON.stringify(result) }]; } else if (typeof result === 'string') { const parsedContent = isFromMcpTool ? parseMcpContentArrayString(result) : null; content = parsedContent ?? [{ type: 'text', text: result }]; } else if (result === null || result === undefined) { content = [{ type: 'text', text: String(result) }]; } else if (typeof result === 'number' || typeof result === 'boolean' || typeof result === 'bigint') { content = [{ type: 'text', text: result.toString() }]; } else { content = [ { type: 'text', text: String(result), }, ]; } return isError ? { isError: true, content } : { content }; } static isErrorResult(result) { return (0, tool_error_result_1.isToolErrorResult)(result); } static formatError(error) { return { isError: true, content: [{ type: 'text', text: `${error.name}: ${error.message}` }], }; } static formatCredentialGate(result) { const missing = result.credentials.filter((c) => c.status !== 'configured'); const lines = missing.flatMap((c) => { const label = `- ${c.credentialName} (${c.credentialType})`; return c.authorizationUrl ? [label, c.authorizationUrl] : [`${label}: not connected`]; }); const text = [ 'This tool requires credentials that are not connected for your account yet.', 'Connect each of the following, then retry the request:', ...lines, ].join('\n'); return { isError: true, content: [{ type: 'text', text }], credentialGate: result, }; } static formatCredentialGateElicited(outcomes) { const opened = outcomes.filter((o) => o.action === 'accept'); const skipped = outcomes.filter((o) => o.action !== 'accept'); const lines = []; if (opened.length) { lines.push('A connection page was opened for the following credential(s). Once connected, retry the request:', ...opened.map((o) => `- ${o.credentialName} (${o.credentialType})`)); } if (skipped.length) { lines.push('These credentials still need to be connected before the tool can run:', ...skipped.map((o) => `- ${o.credentialName} (${o.credentialType})`)); } const content = [{ type: 'text', text: lines.join('\n') }]; return skipped.length > 0 ? { isError: true, content } : { content }; } } exports.MessageFormatter = MessageFormatter; //# sourceMappingURL=MessageFormatter.js.map