@jeelidev/personal-neo4j-memory-server
Version:
Personal MCP Memory Server with Neo4j backend - Enhanced Cloudflare Access support with robust tunnel management and remove functionality
2 lines (1 loc) • 13.5 kB
JavaScript
import{c as m,d as c,g as d}from"./chunk-R5RY4KXE.mjs";import{a as i,c as r,d as p,g as w}from"./chunk-ULWSXDW6.mjs";var I=class{constructor(){this.initializationPromise=null;this.container=d.getInstance()}async ensureDatabaseInitialized(){return this.initializationPromise||(this.initializationPromise=this.container.initializeDatabase().catch(e=>{throw this.initializationPromise=null,e})),this.initializationPromise}async handleMemoryManage(e){this.validateMemoryManageRequest(e),await this.ensureDatabaseInitialized();let a=this.container.getCurrentDatabase();switch(e.operation){case"create":return this.handleCreateMemories(e.memories,a);case"update":return this.handleUpdateMemories(e.updates,a);case"delete":return this.handleDeleteMemories(e.identifiers,a);default:throw new w(`Invalid operation: ${e.operation}. Valid operations: create, update, delete`,i.VALIDATION_FAILED)}}async handleMemoryRetrieve(e){this.validateIdentifiers(e),await this.ensureDatabaseInitialized();let a=this.container.getCurrentDatabase(),n=await this.container.getMemoryRepository().findByIds(e);return{memories:n.map(o=>this.stripEmbeddings(o)),_meta:{database:a.database,retrieved:n.length}}}async handleMemorySearch(e,a,t,n,o){this.validateSearchRequest(e,a,o),await this.ensureDatabaseInitialized();let s=this.container.getCurrentDatabase(),g=await this.container.getSearchMemoriesUseCase().execute({query:e,limit:a,includeGraphContext:t,memoryTypes:n,threshold:o});return{memories:g.map(A=>({...this.stripEmbeddings(A.memory),score:A.score})),_meta:{database:s.database,total:g.length,query:e,queryTime:Date.now()}}}async handleCreateMemories(e,a){if(!e||!Array.isArray(e)||e.length===0)throw new r("memories array cannot be empty",i.EMPTY_ARRAY);let t=this.container.getCreateMemoryUseCase(),n=[];for(let o of e)try{let s=await t.execute(o);n.push({id:s.id,status:"created"})}catch(s){n.push({id:o.name||"unknown",status:"failed",error:c("Failed to create memory",s)})}return{success:!0,results:n,summary:{requested:e.length,succeeded:n.filter(o=>o.status==="created").length,failed:n.filter(o=>o.status==="failed").length},_meta:{database:a.database,operation:"create",timestamp:new Date().toISOString()}}}async handleUpdateMemories(e,a){if(!e||!Array.isArray(e)||e.length===0)throw new r("updates array cannot be empty",i.EMPTY_ARRAY);let t=this.container.getUpdateMemoryUseCase(),n=[];for(let o of e)try{let s=await t.execute(o);n.push({id:s.id,status:"updated"})}catch(s){n.push({id:o.id||"unknown",status:"failed",error:c("Failed to update memory",s)})}return{success:!0,results:n,summary:{requested:e.length,succeeded:n.filter(o=>o.status==="updated").length,failed:n.filter(o=>o.status==="failed").length},_meta:{database:a.database,operation:"update",timestamp:new Date().toISOString()}}}async handleDeleteMemories(e,a){if(!e||!Array.isArray(e)||e.length===0)throw new r("identifiers array cannot be empty",i.EMPTY_ARRAY);let t=this.container.getDeleteMemoryUseCase(),n=[];for(let o of e)try{await t.execute(o),n.push({id:o,status:"deleted"})}catch(s){n.push({id:o,status:"failed",error:c("Failed to delete memory",s)})}return{success:!0,results:n,summary:{requested:e.length,succeeded:n.filter(o=>o.status==="deleted").length,failed:n.filter(o=>o.status==="failed").length},_meta:{database:a.database,operation:"delete",timestamp:new Date().toISOString()}}}stripEmbeddings(e){let{nameEmbedding:a,...t}=e;return t}validateMemoryManageRequest(e){if(!e.operation)throw new r("Operation is required",i.VALIDATION_FAILED);switch(e.operation){case"create":if(!e.memories||!Array.isArray(e.memories)||e.memories.length===0)throw new r("Create operation requires non-empty memories array",i.EMPTY_ARRAY);this.validateMemoryCreationData(e.memories);break;case"update":if(!e.updates||!Array.isArray(e.updates)||e.updates.length===0)throw new r("Update operation requires non-empty updates array",i.EMPTY_ARRAY);this.validateMemoryUpdateData(e.updates);break;case"delete":if(!e.identifiers||!Array.isArray(e.identifiers)||e.identifiers.length===0)throw new r("Delete operation requires non-empty identifiers array",i.EMPTY_ARRAY);this.validateIdentifiers(e.identifiers);break}}validateMemoryCreationData(e){for(let a=0;a<e.length;a++){let t=e[a];if(!t||typeof t!="object")throw new r(`Memory at index ${a} must be an object`,i.VALIDATION_FAILED);if(!t.name||typeof t.name!="string"||t.name.trim().length===0)throw new r(`Memory at index ${a} must have a non-empty name`,i.INVALID_NAME);if(!t.memoryType||typeof t.memoryType!="string"||t.memoryType.trim().length===0)throw new r(`Memory at index ${a} must have a non-empty memoryType`,i.INVALID_TYPE)}}validateMemoryUpdateData(e){for(let a=0;a<e.length;a++){let t=e[a];if(!t||typeof t!="object")throw new r(`Update at index ${a} must be an object`,i.VALIDATION_FAILED);if(!t.id||typeof t.id!="string"||t.id.trim().length===0)throw new r(`Update at index ${a} must have a non-empty id`,i.INVALID_ID_FORMAT);if(t.id.length!==18)throw new r(`Update at index ${a} has invalid id format (expected 18 characters)`,i.INVALID_MEMORY_ID_LENGTH)}}validateIdentifiers(e){if(!Array.isArray(e))throw new r("Identifiers must be an array",i.VALIDATION_FAILED);if(e.length===0)throw new r("Identifiers array cannot be empty",i.EMPTY_ARRAY);for(let a=0;a<e.length;a++){let t=e[a];if(!t||typeof t!="string"||t.trim().length===0)throw new r(`Identifier at index ${a} must be a non-empty string`,i.INVALID_ID_FORMAT);if(t.length!==18)throw new r(`Identifier at index ${a} has invalid format (expected 18 characters)`,i.INVALID_MEMORY_ID_LENGTH)}}validateSearchRequest(e,a,t){if(!e||typeof e!="string"||e.trim().length===0)throw new r("Search query must be a non-empty string",i.EMPTY_QUERY);if(!Number.isInteger(a)||a<=0)throw new r("Search limit must be a positive integer",i.INVALID_LIMIT);if(a>1e3)throw new r("Search limit cannot exceed 1000",i.INVALID_LIMIT);if(t!==void 0&&(typeof t!="number"||t<0||t>1))throw new r("Search threshold must be a number between 0 and 1",i.INVALID_THRESHOLD)}};var y=class{constructor(){this.initializationPromise=null;this.container=d.getInstance()}async ensureDatabaseInitialized(){return this.initializationPromise||(this.initializationPromise=this.container.initializeDatabase().catch(e=>{throw this.initializationPromise=null,e})),this.initializationPromise}async handleObservationManage(e){this.validateObservationRequest(e),await this.ensureDatabaseInitialized();let a=this.container.getCurrentDatabase(),t=this.container.getManageObservationsUseCase(),n=[],o=0;for(let s of e.observations)try{let l=await t.executeMany(e.operation,[s]);l.errors.length===0?(n.push({memoryId:s.memoryId,status:"success",observations:{requested:s.contents.length,processed:s.contents.length}}),o+=s.contents.length):n.push({memoryId:s.memoryId,status:"failed",error:l.errors.join("; ")})}catch(l){n.push({memoryId:s.memoryId,status:"failed",error:m(l)})}return{success:!0,results:n,summary:{memories_processed:n.filter(s=>s.status==="success").length,memories_failed:n.filter(s=>s.status==="failed").length,observations_processed:o},_meta:{database:a.database,operation:e.operation,timestamp:new Date().toISOString()}}}validateObservationRequest(e){if(!e.operation)throw new r("Operation is required",i.VALIDATION_FAILED);if(!["add","delete"].includes(e.operation))throw new r(`Invalid operation: ${e.operation}. Valid operations: add, delete`,i.VALIDATION_FAILED);if(!e.observations||!Array.isArray(e.observations)||e.observations.length===0)throw new r("Observations array is required and cannot be empty",i.EMPTY_ARRAY);for(let a=0;a<e.observations.length;a++){let t=e.observations[a];if(!t||typeof t!="object")throw new r(`Observation at index ${a} must be an object`,i.VALIDATION_FAILED);if(!t.memoryId||typeof t.memoryId!="string"||t.memoryId.trim().length===0)throw new r(`Observation at index ${a} must have a non-empty memoryId`,i.INVALID_ID_FORMAT);if(t.memoryId.length!==18)throw new r(`Observation at index ${a} has invalid memoryId format (expected 18 characters)`,i.INVALID_MEMORY_ID_LENGTH);if(!t.contents||!Array.isArray(t.contents)||t.contents.length===0)throw new r(`Observation at index ${a} must have a non-empty contents array`,i.EMPTY_ARRAY);e.operation==="add"?this.validateObservationContentsForAdd(t.contents,a):e.operation==="delete"&&this.validateObservationContentsForDelete(t.contents,a)}}validateObservationContentsForAdd(e,a){for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="string"||n.trim().length===0)throw new r(`Observation at index ${a}, content at index ${t} must be a non-empty string`,i.INVALID_OBSERVATION_CONTENT)}}validateObservationContentsForDelete(e,a){for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="string"||n.trim().length===0)throw new r(`Observation at index ${a}, ID at index ${t} must be a non-empty string`,i.INVALID_ID_FORMAT);if(n.length!==18)throw new r(`Observation at index ${a}, ID at index ${t} has invalid format (expected 18 characters)`,i.INVALID_MEMORY_ID_LENGTH)}}};var f=class{constructor(){this.initializationPromise=null;this.container=d.getInstance()}async ensureDatabaseInitialized(){return this.initializationPromise||(this.initializationPromise=this.container.initializeDatabase().catch(e=>{throw this.initializationPromise=null,e})),this.initializationPromise}async handleRelationManage(e){this.validateRelationRequest(e),await this.ensureDatabaseInitialized();let a=this.container.getCurrentDatabase(),t=this.container.getManageRelationsUseCase(),n=[];for(let o of e.relations)try{e.operation==="create"?await t.createRelation(o):await t.deleteRelation(o),n.push({fromId:o.fromId,toId:o.toId,relationType:o.relationType,status:e.operation==="create"?"created":"deleted"})}catch(s){n.push({fromId:o.fromId,toId:o.toId,relationType:o.relationType,status:"failed",error:m(s)})}return{success:!0,results:n,summary:{requested:e.relations.length,succeeded:n.filter(o=>o.status==="created"||o.status==="deleted").length,failed:n.filter(o=>o.status==="failed").length},_meta:{database:a.database,operation:e.operation,timestamp:new Date().toISOString()}}}validateRelationRequest(e){if(!e.operation)throw new r("Operation is required",i.VALIDATION_FAILED);if(!["create","delete"].includes(e.operation))throw new r(`Invalid operation: ${e.operation}. Valid operations: create, delete`,i.VALIDATION_FAILED);if(!e.relations||!Array.isArray(e.relations)||e.relations.length===0)throw new r("Relations array is required and cannot be empty",i.EMPTY_ARRAY);for(let a=0;a<e.relations.length;a++){let t=e.relations[a];if(!t||typeof t!="object")throw new r(`Relation at index ${a} must be an object`,i.VALIDATION_FAILED);if(!t.fromId||typeof t.fromId!="string"||t.fromId.trim().length===0)throw new r(`Relation at index ${a} must have a non-empty fromId`,i.INVALID_ID_FORMAT);if(t.fromId.length!==18)throw new r(`Relation at index ${a} has invalid fromId format (expected 18 characters)`,i.INVALID_MEMORY_ID_LENGTH);if(!t.toId||typeof t.toId!="string"||t.toId.trim().length===0)throw new r(`Relation at index ${a} must have a non-empty toId`,i.INVALID_ID_FORMAT);if(t.toId.length!==18)throw new r(`Relation at index ${a} has invalid toId format (expected 18 characters)`,i.INVALID_MEMORY_ID_LENGTH);if(!t.relationType||typeof t.relationType!="string"||t.relationType.trim().length===0)throw new r(`Relation at index ${a} must have a non-empty relationType`,i.INVALID_TYPE);if(t.strength!==void 0&&(typeof t.strength!="number"||t.strength<0||t.strength>1))throw new r(`Relation at index ${a} has invalid strength (must be a number between 0 and 1)`,i.INVALID_STRENGTH);if(t.source!==void 0&&!["agent","user","system"].includes(t.source))throw new r(`Relation at index ${a} has invalid source (must be 'agent', 'user', or 'system')`,i.VALIDATION_FAILED);if(t.fromId===t.toId)throw new r(`Relation at index ${a} cannot be self-referencing (fromId and toId are the same)`,i.INVALID_SELF_REFERENCE)}}};var u=class{constructor(){this.initializationPromise=null;this.container=d.getInstance()}async ensureDatabaseInitialized(){return this.initializationPromise||(this.initializationPromise=this.container.initializeDatabase().catch(e=>{throw this.initializationPromise=null,e})),this.initializationPromise}async handleDatabaseSwitch(e){this.validateDatabaseName(e),await this.ensureDatabaseInitialized();try{return await this.container.getDatabaseManager().switchDatabase(e)}catch(a){if(a instanceof Error){if(a.message.includes("Invalid database name"))throw new r(`Invalid database name: ${e}`,i.INVALID_DATABASE_NAME,{databaseName:e,reason:"invalid_name"});if(a.message.includes("Connection failed")||a.message.includes("ServiceUnavailable"))throw new p(`Database connection failed: ${a.message}`,i.DATABASE_UNAVAILABLE)}throw new p(`Database switch to '${e}' failed: ${a instanceof Error?a.message:String(a)}`,i.DATABASE_OPERATION_FAILED)}}validateDatabaseName(e){if(!e||typeof e!="string")throw new r("Database name must be a non-empty string",i.INVALID_DATABASE_NAME);if(e.trim()!==e)throw new r("Database name cannot have leading or trailing whitespace",i.INVALID_DATABASE_NAME);if(e.length===0)throw new r("Database name cannot be empty",i.INVALID_DATABASE_NAME);if(e.length>63)throw new r("Database name cannot exceed 63 characters",i.INVALID_DATABASE_NAME,{length:e.length,maxLength:63});if(!/^[a-zA-Z0-9\s\-_]+$/.test(e))throw new r("Database name contains invalid characters. Only letters, numbers, spaces, hyphens, and underscores are allowed",i.INVALID_DATABASE_NAME,{invalidName:e})}};export{I as a,y as b,f as c,u as d};