Skip to content

Commit 72e1111

Browse files
authored
Merge pull request #3213 from SashankMeka1/kg-type-loading-fix
remove type from loading
2 parents 861c11b + 8af50b9 commit 72e1111

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

src/memory/__tests__/knowledge-graph.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,5 +390,94 @@ describe('KnowledgeGraphManager', () => {
390390
expect(JSON.parse(lines[0])).toHaveProperty('type', 'entity');
391391
expect(JSON.parse(lines[1])).toHaveProperty('type', 'relation');
392392
});
393+
394+
it('should strip type field from entities when loading from file', async () => {
395+
// Create entities and relations (these get saved with type field)
396+
await manager.createEntities([
397+
{ name: 'Alice', entityType: 'person', observations: ['test observation'] },
398+
{ name: 'Bob', entityType: 'person', observations: [] },
399+
]);
400+
await manager.createRelations([
401+
{ from: 'Alice', to: 'Bob', relationType: 'knows' },
402+
]);
403+
404+
// Verify file contains type field (order may vary)
405+
const fileContent = await fs.readFile(testFilePath, 'utf-8');
406+
const fileLines = fileContent.split('\n').filter(line => line.trim());
407+
const fileItems = fileLines.map(line => JSON.parse(line));
408+
const fileEntity = fileItems.find(item => item.type === 'entity');
409+
const fileRelation = fileItems.find(item => item.type === 'relation');
410+
expect(fileEntity).toBeDefined();
411+
expect(fileEntity).toHaveProperty('type', 'entity');
412+
expect(fileRelation).toBeDefined();
413+
expect(fileRelation).toHaveProperty('type', 'relation');
414+
415+
// Create new manager instance to force reload from file
416+
const manager2 = new KnowledgeGraphManager(testFilePath);
417+
const graph = await manager2.readGraph();
418+
419+
// Verify loaded entities don't have type field
420+
expect(graph.entities).toHaveLength(2);
421+
graph.entities.forEach(entity => {
422+
expect(entity).not.toHaveProperty('type');
423+
expect(entity).toHaveProperty('name');
424+
expect(entity).toHaveProperty('entityType');
425+
expect(entity).toHaveProperty('observations');
426+
});
427+
428+
// Verify loaded relations don't have type field
429+
expect(graph.relations).toHaveLength(1);
430+
graph.relations.forEach(relation => {
431+
expect(relation).not.toHaveProperty('type');
432+
expect(relation).toHaveProperty('from');
433+
expect(relation).toHaveProperty('to');
434+
expect(relation).toHaveProperty('relationType');
435+
});
436+
});
437+
438+
it('should strip type field from searchNodes results', async () => {
439+
await manager.createEntities([
440+
{ name: 'Alice', entityType: 'person', observations: ['works at Acme'] },
441+
]);
442+
await manager.createRelations([
443+
{ from: 'Alice', to: 'Alice', relationType: 'self' },
444+
]);
445+
446+
// Create new manager instance to force reload from file
447+
const manager2 = new KnowledgeGraphManager(testFilePath);
448+
const result = await manager2.searchNodes('Alice');
449+
450+
// Verify search results don't have type field
451+
expect(result.entities).toHaveLength(1);
452+
expect(result.entities[0]).not.toHaveProperty('type');
453+
expect(result.entities[0].name).toBe('Alice');
454+
455+
expect(result.relations).toHaveLength(1);
456+
expect(result.relations[0]).not.toHaveProperty('type');
457+
expect(result.relations[0].from).toBe('Alice');
458+
});
459+
460+
it('should strip type field from openNodes results', async () => {
461+
await manager.createEntities([
462+
{ name: 'Alice', entityType: 'person', observations: [] },
463+
{ name: 'Bob', entityType: 'person', observations: [] },
464+
]);
465+
await manager.createRelations([
466+
{ from: 'Alice', to: 'Bob', relationType: 'knows' },
467+
]);
468+
469+
// Create new manager instance to force reload from file
470+
const manager2 = new KnowledgeGraphManager(testFilePath);
471+
const result = await manager2.openNodes(['Alice', 'Bob']);
472+
473+
// Verify open results don't have type field
474+
expect(result.entities).toHaveLength(2);
475+
result.entities.forEach(entity => {
476+
expect(entity).not.toHaveProperty('type');
477+
});
478+
479+
expect(result.relations).toHaveLength(1);
480+
expect(result.relations[0]).not.toHaveProperty('type');
481+
});
393482
});
394483
});

src/memory/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,20 @@ export class KnowledgeGraphManager {
7474
const lines = data.split("\n").filter(line => line.trim() !== "");
7575
return lines.reduce((graph: KnowledgeGraph, line) => {
7676
const item = JSON.parse(line);
77-
if (item.type === "entity") graph.entities.push(item as Entity);
78-
if (item.type === "relation") graph.relations.push(item as Relation);
77+
if (item.type === "entity") {
78+
graph.entities.push({
79+
name: item.name,
80+
entityType: item.entityType,
81+
observations: item.observations
82+
});
83+
}
84+
if (item.type === "relation") {
85+
graph.relations.push({
86+
from: item.from,
87+
to: item.to,
88+
relationType: item.relationType
89+
});
90+
}
7991
return graph;
8092
}, { entities: [], relations: [] });
8193
} catch (error) {

0 commit comments

Comments
 (0)