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
30 changes: 30 additions & 0 deletions src/memory/__tests__/knowledge-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ describe('KnowledgeGraphManager', () => {
const newRelations = await manager.createRelations([]);
expect(newRelations).toHaveLength(0);
});

it('should reject relations with a missing from entity', async () => {
await manager.createEntities([
{ name: 'Alice', entityType: 'person', observations: [] },
]);

await expect(
manager.createRelations([
{ from: 'Ghost', to: 'Alice', relationType: 'knows' },
])
).rejects.toThrow('Entity with name Ghost not found');

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

it('should reject relations with a missing to entity', async () => {
await manager.createEntities([
{ name: 'Alice', entityType: 'person', observations: [] },
]);

await expect(
manager.createRelations([
{ from: 'Alice', to: 'Ghost', relationType: 'knows' },
])
).rejects.toThrow('Entity with name Ghost not found');

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

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

async createRelations(relations: Relation[]): Promise<Relation[]> {
const graph = await this.loadGraph();
const entityNames = new Set(graph.entities.map(e => e.name));

for (const relation of relations) {
if (!entityNames.has(relation.from)) {
throw new Error(`Entity with name ${relation.from} not found`);
}
if (!entityNames.has(relation.to)) {
throw new Error(`Entity with name ${relation.to} not found`);
}
}

const newRelations = relations.filter(r => !graph.relations.some(existingRelation =>
existingRelation.from === r.from &&
existingRelation.to === r.to &&
Expand Down
Loading