UNPKG

n8n

Version:

n8n Workflow Automation Tool

76 lines 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KNOWLEDGE_MIRROR_MANIFEST = exports.KNOWLEDGE_MIRROR_FILES_DIR = exports.KNOWLEDGE_MIRROR_DIR = void 0; exports.hasControlCharacter = hasControlCharacter; exports.buildKnowledgeFileLocation = buildKnowledgeFileLocation; exports.assertKnowledgePathSegment = assertKnowledgePathSegment; exports.storageFileNameForOriginalFileName = storageFileNameForOriginalFileName; exports.toAgentFileDto = toAgentFileDto; const n8n_core_1 = require("n8n-core"); const node_path_1 = __importDefault(require("node:path")); const bad_request_error_1 = require("../../errors/response-errors/bad-request.error"); exports.KNOWLEDGE_MIRROR_DIR = '/home/daytona/knowledge-mirror'; exports.KNOWLEDGE_MIRROR_FILES_DIR = `${exports.KNOWLEDGE_MIRROR_DIR}/files`; exports.KNOWLEDGE_MIRROR_MANIFEST = `${exports.KNOWLEDGE_MIRROR_DIR}/manifest`; const AGENT_FILE_SOURCE_TYPE = 'agent_file'; function hasControlCharacter(value) { for (const character of value) { if (character.charCodeAt(0) < 32) { return true; } } return false; } function sanitizePathCharacter(character) { if (character === '/' || character === '\\' || character.charCodeAt(0) < 32) { return '_'; } return character; } function buildKnowledgeFileLocation(agentId, fileId) { return n8n_core_1.FileLocation.ofCustom({ pathSegments: ['agents', agentId, 'knowledge-files', fileId], sourceType: AGENT_FILE_SOURCE_TYPE, sourceId: fileId, }); } function assertKnowledgePathSegment(segment, label) { if (!segment || segment === '.' || segment === '..' || /[\\/]/.test(segment) || hasControlCharacter(segment)) { throw new Error(`Invalid ${label} for agent knowledge storage`); } } function sanitizeStorageFileName(originalName) { const basename = node_path_1.default.basename(originalName); const sanitized = Array.from(basename, sanitizePathCharacter).join(''); if (!sanitized || sanitized === '.' || sanitized === '..') { throw new bad_request_error_1.BadRequestError(`Invalid knowledge file name "${originalName}"`); } return sanitized; } function storageFileNameForOriginalFileName(originalFileName) { const sanitizedName = sanitizeStorageFileName(originalFileName); const extension = node_path_1.default.extname(sanitizedName).toLowerCase(); if (extension === '.pdf') { const baseName = node_path_1.default.basename(sanitizedName, node_path_1.default.extname(sanitizedName)); return `${baseName}.txt`; } return sanitizedName; } function toAgentFileDto(file) { return { id: file.id, agentId: file.agentId, fileName: file.fileName, mimeType: file.mimeType, fileSizeBytes: file.fileSizeBytes, createdAt: file.createdAt.toISOString(), }; } //# sourceMappingURL=agent-knowledge-storage.js.map