@n8n/n8n-nodes-langchain
Version:
119 lines • 5.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MODEL_OUTPUT_PARSER_ERROR_DESCRIPTION = exports.MODEL_OUTPUT_PARSER_ERROR_MESSAGE = void 0;
exports.isLangChainParserError = isLangChainParserError;
exports.getFailureType = getFailureType;
exports.wrapLangChainParserError = wrapLangChainParserError;
const output_parsers_1 = require("@langchain/core/output_parsers");
const sanitize_error_detail_1 = require("@n8n/utils/redaction/sanitize-error-detail");
const n8n_workflow_1 = require("n8n-workflow");
exports.MODEL_OUTPUT_PARSER_ERROR_MESSAGE = "Model output doesn't fit required format";
exports.MODEL_OUTPUT_PARSER_ERROR_DESCRIPTION = "To continue the execution when this happens, change the 'On Error' parameter in the root node's settings";
const LANGCHAIN_PARSER_ERROR_MESSAGES = ['Failed to parse. Text:', 'Unable to parse JSON response'];
const AGENT_FAILURE_FALLBACK_MESSAGE = 'Agent execution failed';
const MAX_CAUSE_DETAIL_LENGTH = 500;
function getErrorProperty(error, property) {
if (typeof error !== 'object' || error === null || !(property in error))
return undefined;
const value = error[property];
return typeof value === 'string' ? value : undefined;
}
function resolveErrorName(error) {
if (error instanceof Error) {
if (error.name && error.name !== 'Error')
return error.name;
return error.constructor.name || undefined;
}
return getErrorProperty(error, 'name');
}
function hasLangChainParserMessage(error) {
const errorMessage = getErrorProperty(error, 'message');
if (!errorMessage)
return false;
return LANGCHAIN_PARSER_ERROR_MESSAGES.some((message) => errorMessage.includes(message));
}
function isLangChainParserError(error) {
return (error instanceof output_parsers_1.OutputParserException ||
resolveErrorName(error) === 'OutputParserException' ||
hasLangChainParserMessage(error));
}
function getFailureType(error) {
const root = getRootCause(error);
return resolveErrorName(root) ?? (root instanceof Error ? 'Error' : typeof root);
}
function getRootCause(error) {
let current = error;
const seen = new Set();
while (current instanceof Error && current.cause instanceof Error) {
seen.add(current);
if (seen.has(current.cause))
break;
current = current.cause;
}
return current;
}
function isUselessMessage(message, className) {
if (!message)
return true;
if (className && message === className)
return true;
return message === 'Error';
}
function describeOriginalError(error, className) {
const original = className ? `Original error: ${className}` : undefined;
const root = getRootCause(error);
if (root === error || !(root instanceof Error))
return original;
const code = 'code' in root && typeof root.code === 'string' ? root.code : undefined;
const detail = code && !root.message.includes(code)
? [code, root.message].filter(Boolean).join(': ')
: root.message;
if (!detail)
return original;
const cause = `Cause: ${(0, sanitize_error_detail_1.sanitizeErrorDetail)(detail, MAX_CAUSE_DETAIL_LENGTH)}`;
return original ? `${original}. ${cause}` : cause;
}
function wrapAsNodeOperationError(error, node, itemIndex, fallbackMessage) {
const className = resolveErrorName(error);
const messageProperty = getErrorProperty(error, 'message');
const candidateMessage = messageProperty ?? (typeof error === 'string' ? error : undefined);
const message = isUselessMessage(candidateMessage, className)
? fallbackMessage
: candidateMessage;
const options = {
message,
description: getErrorProperty(error, 'description') ?? describeOriginalError(error, className),
level: 'error',
...(itemIndex !== undefined ? { itemIndex } : {}),
};
let cause;
if (error instanceof Error) {
cause = error;
}
else {
cause = new Error(message);
if (className)
cause.name = className;
}
return new n8n_workflow_1.NodeOperationError(node, cause, options);
}
function wrapLangChainParserError(error, node, itemIndex, options) {
if (!isLangChainParserError(error)) {
if (!options?.enrichNonParserErrors) {
return error instanceof Error
? error
: new Error(getErrorProperty(error, 'message') ?? String(error));
}
if (error instanceof n8n_workflow_1.BaseError)
return error;
return wrapAsNodeOperationError(error, node, itemIndex, options.fallbackMessage ?? AGENT_FAILURE_FALLBACK_MESSAGE);
}
const parserOptions = {
description: exports.MODEL_OUTPUT_PARSER_ERROR_DESCRIPTION,
...(itemIndex !== undefined ? { itemIndex } : {}),
};
const nodeError = new n8n_workflow_1.NodeOperationError(node, exports.MODEL_OUTPUT_PARSER_ERROR_MESSAGE, parserOptions);
nodeError.context.outputParserFailReason = 'Model output does not match the expected schema';
return nodeError;
}
//# sourceMappingURL=langchainParserError.js.map