UNPKG

taskforce-aiagent

Version:

TaskForce is a modular, open-source, production-ready TypeScript agent framework for orchestrating AI agents, LLM-powered autonomous agents, task pipelines, dynamic toolchains, RAG workflows and memory/retrieval systems.

82 lines (81 loc) 3.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChromaVectorMemoryProvider = void 0; const chromadb_1 = require("chromadb"); const enum_js_1 = require("../../../configs/enum.js"); const summarize_helper_js_1 = require("../../../helpers/summarize.helper.js"); const dotenv_1 = __importDefault(require("dotenv")); const helper_js_1 = require("../../../helpers/helper.js"); dotenv_1.default.config(); class ChromaVectorMemoryProvider { constructor(collectionName) { this.collectionName = collectionName && collectionName != "" ? collectionName : process.env.CHROMA_COLLECTION_NAME ?? "agent_memory"; this.client = new chromadb_1.ChromaClient({ path: process.env.CHROMA_DB_PATH }); } async getCollection() { if (!this.collectionName) { throw new Error("Collection name is not defined"); } return await this.client.getOrCreateCollection({ name: this.collectionName, }); } async storeMemory(record) { const collection = await this.getCollection(); const summary = await (0, summarize_helper_js_1.generateMemorySummary)(record.output); const normalizedInput = (0, helper_js_1.normalizeInput)(record.input); // Normalize input await collection.add({ ids: [record.taskId], documents: [record.output], metadatas: [{ ...record.metadata, input: normalizedInput, summary }], }); } async loadRelevantMemory(query, limit = 3, filter) { const collection = await this.getCollection(); const result = await collection.query({ queryTexts: [query], nResults: limit, where: { ...(filter?.agent ? { agent: filter.agent } : {}), ...(filter?.taskId ? { taskId: filter.taskId } : {}), }, }); const records = (result.documents ?? []).map((docGroup, i) => { const raw = result.metadatas?.[i]; const metadata = Array.isArray(raw) && raw.length > 0 ? raw[0] : typeof raw === "object" && raw !== null ? raw : {}; console.log("🧪 [Chroma DEBUG] raw:", raw); return { taskId: Array.isArray(result.ids?.[i]) ? String(result.ids[i][0] ?? "") : String(result.ids?.[i] ?? ""), input: metadata.input ?? query, output: Array.isArray(docGroup) ? docGroup[0] : String(docGroup ?? ""), metadata, summary: metadata.summary, }; }); return records; } async clearMemory() { if (!this.collectionName) { throw new Error("Collection name is not defined"); } await this.client.deleteCollection({ name: this.collectionName }); } getMemoryScope() { return enum_js_1.MemoryScope.Long; } } exports.ChromaVectorMemoryProvider = ChromaVectorMemoryProvider;