knowledge-graph.ts•4.17 kB
import { Storage } from './storage.js';
import { Entity, Relation, KnowledgeGraph, ObservationInput, ObservationOutput, DeletionInput } from './types.js';
export class KnowledgeGraphManager {
private storage: Storage;
constructor() {
this.storage = new Storage();
}
async createEntities(entities: Entity[]): Promise<Entity[]> {
const graph = await this.storage.loadGraph();
const newEntities = entities.filter(e => !graph.entities.some(existingEntity => existingEntity.name === e.name));
graph.entities.push(...newEntities);
await this.storage.saveGraph(graph);
return newEntities;
}
async createRelations(relations: Relation[]): Promise<Relation[]> {
const graph = await this.storage.loadGraph();
const newRelations = relations.filter(r => !graph.relations.some(existingRelation =>
existingRelation.from === r.from &&
existingRelation.to === r.to &&
existingRelation.relationType === r.relationType
));
graph.relations.push(...newRelations);
await this.storage.saveGraph(graph);
return newRelations;
}
async addObservations(observations: ObservationInput[]): Promise<ObservationOutput[]> {
const graph = await this.storage.loadGraph();
const results = observations.map(o => {
const entity = graph.entities.find(e => e.name === o.entityName);
if (!entity) {
throw new Error(`Entity with name ${o.entityName} not found`);
}
const newObservations = o.contents.filter(content => !entity.observations.includes(content));
entity.observations.push(...newObservations);
return { entityName: o.entityName, addedObservations: newObservations };
});
await this.storage.saveGraph(graph);
return results;
}
async deleteEntities(entityNames: string[]): Promise<void> {
const graph = await this.storage.loadGraph();
graph.entities = graph.entities.filter(e => !entityNames.includes(e.name));
graph.relations = graph.relations.filter(r => !entityNames.includes(r.from) && !entityNames.includes(r.to));
await this.storage.saveGraph(graph);
}
async deleteObservations(deletions: DeletionInput[]): Promise<void> {
const graph = await this.storage.loadGraph();
deletions.forEach(d => {
const entity = graph.entities.find(e => e.name === d.entityName);
if (entity) {
entity.observations = entity.observations.filter(o => !d.observations.includes(o));
}
});
await this.storage.saveGraph(graph);
}
async deleteRelations(relations: Relation[]): Promise<void> {
const graph = await this.storage.loadGraph();
graph.relations = graph.relations.filter(r => !relations.some(delRelation =>
r.from === delRelation.from &&
r.to === delRelation.to &&
r.relationType === delRelation.relationType
));
await this.storage.saveGraph(graph);
}
async readGraph(): Promise<KnowledgeGraph> {
return this.storage.loadGraph();
}
async searchNodes(query: string): Promise<KnowledgeGraph> {
const graph = await this.storage.loadGraph();
const filteredEntities = graph.entities.filter(e =>
e.name.toLowerCase().includes(query.toLowerCase()) ||
e.entityType.toLowerCase().includes(query.toLowerCase()) ||
e.observations.some(o => o.toLowerCase().includes(query.toLowerCase()))
);
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
const filteredRelations = graph.relations.filter(r =>
filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to)
);
return {
entities: filteredEntities,
relations: filteredRelations,
};
}
async openNodes(names: string[]): Promise<KnowledgeGraph> {
const graph = await this.storage.loadGraph();
const filteredEntities = graph.entities.filter(e => names.includes(e.name));
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
const filteredRelations = graph.relations.filter(r =>
filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to)
);
return {
entities: filteredEntities,
relations: filteredRelations,
};
}
}