UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

66 lines • 2.57 kB
import { matchesFilter } from '../filter.js'; import { filterDeniesAll } from '../vector-store.js'; export function headerOf(tree) { return { documentId: tree.documentId, ...(tree.title !== undefined ? { title: tree.title } : {}), ...(tree.description !== undefined ? { description: tree.description } : {}), structure: tree.structure, fingerprint: tree.fingerprint, unitCount: tree.unitCount, ...(tree.metadata !== undefined ? { metadata: tree.metadata } : {}), ...(tree.source !== undefined ? { source: tree.source } : {}), builtAt: tree.builtAt, }; } function passes(tree, filter) { if (filter === undefined || Object.keys(filter).length === 0) { return true; } if (filterDeniesAll(filter)) { return false; } return matchesFilter(tree.metadata, filter); } /** An in-process {@link DocumentTreeStore} — tests, and single-process apps with small corpora. */ export class MemoryDocumentTreeStore { trees = new Map(); async put(tree, units) { this.trees.set(tree.documentId, { tree: structuredClone(tree), units: units.map((unit) => ({ ...unit })), }); } async get(documentId, filter) { const entry = this.trees.get(documentId); return entry !== undefined && passes(entry.tree, filter) ? structuredClone(entry.tree) : undefined; } async getMany(documentIds, filter) { const trees = []; for (const id of new Set(documentIds)) { const tree = await this.get(id, filter); if (tree !== undefined) { trees.push(tree); } } return trees; } async list(options = {}) { const ids = options.documentIds !== undefined ? new Set(options.documentIds) : undefined; const headers = [...this.trees.values()] .filter(({ tree }) => (ids === undefined || ids.has(tree.documentId)) && passes(tree, options.filter)) .map(({ tree }) => headerOf(tree)) .sort((a, b) => a.documentId.localeCompare(b.documentId)); return options.limit !== undefined ? headers.slice(0, options.limit) : headers; } async readUnits(documentId, start, end) { const units = this.trees.get(documentId)?.units ?? []; return units.slice(Math.max(0, start), Math.max(0, end + 1)).map((unit) => ({ ...unit })); } async remove(documentId) { this.trees.delete(documentId); } } //# sourceMappingURL=store.js.map