knowledgegraph-mcp
Version:
MCP server for enabling persistent knowledge storage for Claude through a knowledge graph with multiple storage backends
109 lines • 4.42 kB
JavaScript
/**
* Base class for search strategies with common functionality
*/
export class BaseSearchStrategy {
config;
constructor(config) {
this.config = config;
}
/**
* Exact search implementation for backward compatibility
*/
exactSearch(query, entities) {
const lowerQuery = query.toLowerCase();
return entities.filter(e => {
const entityTags = e.tags || [];
return (e.name.toLowerCase().includes(lowerQuery) ||
e.entityType.toLowerCase().includes(lowerQuery) ||
e.observations.some(o => o.toLowerCase().includes(lowerQuery)) ||
entityTags.some(tag => tag.toLowerCase().includes(lowerQuery)));
});
}
/**
* Filter entities by exact tags
*/
filterByExactTags(entities, exactTags, tagMatchMode = 'any') {
return entities.filter(entity => {
const entityTags = entity.tags || [];
if (tagMatchMode === 'all') {
return exactTags.every(tag => entityTags.includes(tag));
}
else {
return exactTags.some(tag => entityTags.includes(tag));
}
});
}
/**
* Helper method to handle multiple queries with deduplication
*/
async searchMultipleDatabase(queries, threshold, project) {
let allResults = [];
const existingEntityNames = new Set();
for (const query of queries) {
const results = await this.searchDatabase(query, threshold, project);
// Add only new entities (deduplication)
const newEntities = results.filter(e => !existingEntityNames.has(e.name));
allResults.push(...newEntities);
// Update the set of existing entity names
newEntities.forEach(e => existingEntityNames.add(e.name));
}
return allResults;
}
/**
* Helper method to handle multiple queries for client-side search with deduplication
*/
searchMultipleClientSide(entities, queries) {
let allResults = [];
const existingEntityNames = new Set();
for (const query of queries) {
const results = this.searchClientSide(entities, query);
// Add only new entities (deduplication)
const newEntities = results.filter(e => !existingEntityNames.has(e.name));
allResults.push(...newEntities);
// Update the set of existing entity names
newEntities.forEach(e => existingEntityNames.add(e.name));
}
return allResults;
}
/**
* Chunked client-side search for large entity sets
* Processes entities in chunks to improve performance and memory usage
*/
searchClientSideChunked(entities, query, chunkSize) {
// Handle multiple queries
if (Array.isArray(query)) {
return this.searchMultipleClientSideChunked(entities, query, chunkSize);
}
// Single query chunked processing
let allResults = [];
const existingEntityNames = new Set();
// Process entities in chunks
for (let i = 0; i < entities.length; i += chunkSize) {
const chunk = entities.slice(i, i + chunkSize);
const chunkResults = this.searchClientSide(chunk, query);
// Add only new entities (deduplication)
const newEntities = chunkResults.filter(e => !existingEntityNames.has(e.name));
allResults.push(...newEntities);
// Update the set of existing entity names
newEntities.forEach(e => existingEntityNames.add(e.name));
}
return allResults;
}
/**
* Chunked client-side search for multiple queries
*/
searchMultipleClientSideChunked(entities, queries, chunkSize) {
let allResults = [];
const existingEntityNames = new Set();
for (const query of queries) {
const queryResults = this.searchClientSideChunked(entities, query, chunkSize);
// Add only new entities (deduplication)
const newEntities = queryResults.filter(e => !existingEntityNames.has(e.name));
allResults.push(...newEntities);
// Update the set of existing entity names
newEntities.forEach(e => existingEntityNames.add(e.name));
}
return allResults;
}
}
//# sourceMappingURL=base-strategy.js.map