UNPKG

n8n-nodes-openai-analytics

Version:
225 lines 6.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.memoryCache = exports.MemoryCache = exports.simpleVectorSimilarity = exports.memoryInstance = exports.SimpleMemory = void 0; /** * 임베딩 데이터를 저장하고 관리하는 단순 메모리 클래스 */ class SimpleMemory { constructor() { this.memory = new Map(); } /** * 싱글톤 인스턴스 가져오기 */ static getInstance() { if (!SimpleMemory.instance) { SimpleMemory.instance = new SimpleMemory(); } return SimpleMemory.instance; } /** * 임베딩 데이터 저장하기 * @param key 데이터 식별 키 * @param data 저장할 데이터 */ store(key, data) { this.memory.set(key, data); } /** * 임베딩 데이터 가져오기 * @param key 데이터 식별 키 * @returns 저장된 데이터 또는 undefined (없는 경우) */ retrieve(key) { return this.memory.get(key); } /** * 모든 임베딩 데이터 가져오기 * @returns 모든 저장된 데이터 맵 */ getAll() { return new Map(this.memory); } /** * 특정 키로 시작하는 모든 데이터 가져오기 * @param prefix 키 접두사 * @returns 접두사로 시작하는 모든 데이터 객체의 배열 */ getAllByPrefix(prefix) { const result = []; for (const [key, value] of this.memory.entries()) { if (key.startsWith(prefix)) { result.push(value); } } return result; } /** * 데이터 삭제하기 * @param key 삭제할 데이터의 키 * @returns 삭제 성공 여부 */ remove(key) { return this.memory.delete(key); } /** * 특정 접두사로 시작하는 모든 데이터 삭제하기 * @param prefix 키 접두사 * @returns 삭제된 데이터 수 */ removeByPrefix(prefix) { let count = 0; for (const key of this.memory.keys()) { if (key.startsWith(prefix)) { this.memory.delete(key); count++; } } return count; } /** * 모든 데이터 삭제하기 */ clear() { this.memory.clear(); } /** * 저장된 데이터 수 가져오기 */ size() { return this.memory.size; } } exports.SimpleMemory = SimpleMemory; // 인스턴스 내보내기 exports.memoryInstance = SimpleMemory.getInstance(); /** * 벡터 간 코사인 유사도를 계산합니다. * 두 벡터 간의 코사인 각도를 기반으로 -1과 1 사이의 유사도 값을 반환합니다. * 1에 가까울수록 유사, -1에 가까울수록 반대, 0은 관계 없음을 의미합니다. */ function simpleVectorSimilarity(vecA, vecB) { if (vecA.length !== vecB.length) { throw new Error('벡터 길이가 일치하지 않습니다.'); } let dotProduct = 0; let normA = 0; let normB = 0; for (let i = 0; i < vecA.length; i++) { dotProduct += vecA[i] * vecB[i]; normA += vecA[i] * vecA[i]; normB += vecB[i] * vecB[i]; } if (normA === 0 || normB === 0) { return 0; } return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)); } exports.simpleVectorSimilarity = simpleVectorSimilarity; /** * 간단한 인메모리 캐시 구현체 * 워크플로우 ID별로 키-값 쌍을 저장합니다. */ class MemoryCache { constructor() { this.cache = new Map(); // 싱글톤 패턴을 위한 private 생성자 } static getInstance() { if (!MemoryCache.instance) { MemoryCache.instance = new MemoryCache(); } return MemoryCache.instance; } /** * 지정된 워크플로우 및 키에 대한 값을 저장합니다. */ set(workflowId, key, value) { if (!this.cache.has(workflowId)) { this.cache.set(workflowId, new Map()); } const workflowCache = this.cache.get(workflowId); workflowCache.set(key, value); } /** * 지정된 워크플로우 및 키에 대한 값을 가져옵니다. */ get(workflowId, key) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return undefined; } return workflowCache.get(key); } /** * 지정된 워크플로우 및 키에 대한 값이 있는지 확인합니다. */ has(workflowId, key) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return false; } return workflowCache.has(key); } /** * 지정된 워크플로우 및 키에 대한 값을 삭제합니다. */ delete(workflowId, key) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return false; } return workflowCache.delete(key); } /** * 지정된 워크플로우의 모든 값을 삭제합니다. */ clear(workflowId) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return false; } workflowCache.clear(); return true; } /** * 모든 워크플로우의 모든 값을 삭제합니다. */ clearAll() { this.cache.clear(); } /** * 지정된 워크플로우의 모든 키를 가져옵니다. */ keys(workflowId) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return []; } return Array.from(workflowCache.keys()); } /** * 지정된 워크플로우의 모든 값을 가져옵니다. */ values(workflowId) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return []; } return Array.from(workflowCache.values()); } /** * 지정된 워크플로우의 모든 항목을 가져옵니다. */ entries(workflowId) { const workflowCache = this.cache.get(workflowId); if (!workflowCache) { return []; } return Array.from(workflowCache.entries()); } } exports.MemoryCache = MemoryCache; // 인스턴스 내보내기 exports.memoryCache = MemoryCache.getInstance(); //# sourceMappingURL=simpleMemory.js.map