UNPKG

@knowall-ai/reverie

Version:

Reverie — graph memory that dreams. Graph-based memory system for AI agents. Store people, places, organizations as nodes. Build semantic relationships (KNOWS, WORKS_AT, CREATED). Hybrid keyword + semantic search finds partial matches and close variants.

121 lines (120 loc) • 5.34 kB
/** Labels and relationship types are interpolated into Cypher, so they must be plain identifiers. */ export const IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/; export function isIdentifier(value) { return typeof value === 'string' && IDENTIFIER_RE.test(value); } /** Backtick-quote a validated identifier for use in Cypher. Throws on anything not validated. */ export function cypherIdentifier(value) { if (!isIdentifier(value)) { throw new Error(`Invalid Cypher identifier: ${JSON.stringify(value)}`); } return '`' + value + '`'; } export const SEARCH_MAX_LIMIT = 200; export const SEARCH_MAX_DEPTH = 5; function isPlainObject(value) { return typeof value === 'object' && value !== null && !Array.isArray(value); } function hasOnlyKeys(value, allowed) { return Object.keys(value).every((key) => allowed.includes(key)); } function isIntegerInRange(value, min, max) { return typeof value === 'number' && Number.isInteger(value) && value >= min && value <= max; } const isNodeId = (value) => isIntegerInRange(value, 0, Number.MAX_SAFE_INTEGER); export function isCreateMemoryArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['label', 'properties']) && isIdentifier(args.label) && isPlainObject(args.properties); } const SEARCH_KEYS = ['query', 'label', 'depth', 'order_by', 'limit', 'since_date', 'search_mode', 'similarity_threshold', 'fuzzy_threshold', 'include_archived']; export function isSearchMemoriesArgs(args) { if (!isPlainObject(args) || !hasOnlyKeys(args, SEARCH_KEYS)) return false; const searchArgs = args; if (searchArgs.query !== undefined && typeof searchArgs.query !== 'string') return false; if (searchArgs.label !== undefined && typeof searchArgs.label !== 'string') return false; if (searchArgs.depth !== undefined && !isIntegerInRange(searchArgs.depth, 0, SEARCH_MAX_DEPTH)) return false; if (searchArgs.order_by !== undefined && typeof searchArgs.order_by !== 'string') return false; if (searchArgs.limit !== undefined && !isIntegerInRange(searchArgs.limit, 1, SEARCH_MAX_LIMIT)) return false; if (searchArgs.since_date !== undefined && typeof searchArgs.since_date !== 'string') return false; if (searchArgs.search_mode !== undefined && !['hybrid', 'keyword', 'semantic', 'exact', 'fuzzy'].includes(searchArgs.search_mode)) return false; if (searchArgs.include_archived !== undefined && typeof searchArgs.include_archived !== 'boolean') return false; if (searchArgs.similarity_threshold !== undefined) { const threshold = searchArgs.similarity_threshold; if (typeof threshold !== 'number' || !Number.isFinite(threshold) || threshold < 0 || threshold > 1) return false; } if (searchArgs.fuzzy_threshold !== undefined) { const threshold = searchArgs.fuzzy_threshold; if (typeof threshold !== 'number' || !Number.isFinite(threshold) || threshold < 0 || threshold > 1) return false; } return true; } export function isCreateConnectionArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['fromMemoryId', 'toMemoryId', 'type', 'properties']) && isNodeId(args.fromMemoryId) && isNodeId(args.toMemoryId) && isIdentifier(args.type) && (args.properties === undefined || isPlainObject(args.properties)); } export function isUpdateMemoryArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['nodeId', 'properties']) && isNodeId(args.nodeId) && isPlainObject(args.properties); } export function isUpdateConnectionArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['fromMemoryId', 'toMemoryId', 'type', 'properties']) && isNodeId(args.fromMemoryId) && isNodeId(args.toMemoryId) && isIdentifier(args.type) && isPlainObject(args.properties); } export function isDeleteMemoryArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['nodeId']) && isNodeId(args.nodeId); } export function isDeleteConnectionArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['fromMemoryId', 'toMemoryId', 'type']) && isNodeId(args.fromMemoryId) && isNodeId(args.toMemoryId) && isIdentifier(args.type); } export function isListMemoryLabelsArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, ['include_archived']) && (args.include_archived === undefined || typeof args.include_archived === 'boolean'); } export function isQueryMemoriesArgs(args) { if (!isPlainObject(args) || !hasOnlyKeys(args, ['cypher', 'params'])) return false; const queryArgs = args; if (typeof queryArgs.cypher !== 'string' || queryArgs.cypher.trim() === '') return false; if (queryArgs.params !== undefined && !isPlainObject(queryArgs.params)) return false; return true; } export function isMemoryStatsArgs(args) { return isPlainObject(args) && hasOnlyKeys(args, []); } export function isDreamArgs(args) { if (!isPlainObject(args) || !hasOnlyKeys(args, ['dry_run'])) return false; const dreamArgs = args; if (dreamArgs.dry_run !== undefined && typeof dreamArgs.dry_run !== 'boolean') return false; return true; }