UNPKG

@n8n/n8n-nodes-langchain

Version:
79 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageFormatter = void 0; class MessageFormatter { static formatToolResult(result, isError = false) { let content; if (typeof result === 'object' && result !== null) { content = [{ type: 'text', text: JSON.stringify(result) }]; } else if (typeof result === 'string') { content = [{ 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) { if (typeof result === 'object' && result !== null && 'error' in result) { const { error } = result; return (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string'); } if (typeof result === 'string') { return /^(\w+Error: |HTTP \d{3} There was an error: |There was an error: )/.test(result); } return false; } 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