Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/memory/__tests__/knowledge-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ describe('KnowledgeGraphManager', () => {
const newEntities = await manager.createEntities([]);
expect(newEntities).toHaveLength(0);
});

it('should ignore duplicate entity names within a single batch', async () => {
const entities: Entity[] = [
{ name: 'Alice', entityType: 'person', observations: ['first'] },
{ name: 'Alice', entityType: 'person', observations: ['second'] },
];

const newEntities = await manager.createEntities(entities);
expect(newEntities).toHaveLength(1);

const graph = await manager.readGraph();
expect(graph.entities).toHaveLength(1);
expect(graph.entities[0].name).toBe('Alice');
});
});

describe('createRelations', () => {
Expand Down Expand Up @@ -103,6 +117,24 @@ describe('KnowledgeGraphManager', () => {
const newRelations = await manager.createRelations([]);
expect(newRelations).toHaveLength(0);
});

it('should skip duplicate relations within a single batch', async () => {
await manager.createEntities([
{ name: 'Alice', entityType: 'person', observations: [] },
{ name: 'Bob', entityType: 'person', observations: [] },
]);

const relations: Relation[] = [
{ from: 'Alice', to: 'Bob', relationType: 'knows' },
{ from: 'Alice', to: 'Bob', relationType: 'knows' },
];

const newRelations = await manager.createRelations(relations);
expect(newRelations).toHaveLength(1);

const graph = await manager.readGraph();
expect(graph.relations).toHaveLength(1);
});
});

describe('addObservations', () => {
Expand Down
20 changes: 14 additions & 6 deletions src/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,27 @@ export class KnowledgeGraphManager {

async createEntities(entities: Entity[]): Promise<Entity[]> {
const graph = await this.loadGraph();
const newEntities = entities.filter(e => !graph.entities.some(existingEntity => existingEntity.name === e.name));
const newEntities = entities.filter((e, index) =>
!graph.entities.some(existingEntity => existingEntity.name === e.name) &&
// Also skip duplicates appearing earlier in this same batch
!entities.slice(0, index).some(earlier => earlier.name === e.name)
);
graph.entities.push(...newEntities);
await this.saveGraph(graph);
return newEntities;
}

async createRelations(relations: Relation[]): Promise<Relation[]> {
const graph = await this.loadGraph();
const newRelations = relations.filter(r => !graph.relations.some(existingRelation =>
existingRelation.from === r.from &&
existingRelation.to === r.to &&
existingRelation.relationType === r.relationType
));
const isSameRelation = (a: Relation, b: Relation) =>
a.from === b.from &&
a.to === b.to &&
a.relationType === b.relationType;
const newRelations = relations.filter((r, index) =>
!graph.relations.some(existingRelation => isSameRelation(existingRelation, r)) &&
// Also skip duplicates appearing earlier in this same batch
!relations.slice(0, index).some(earlier => isSameRelation(earlier, r))
);
graph.relations.push(...newRelations);
await this.saveGraph(graph);
return newRelations;
Expand Down
Loading