Skip to content

Commit 23b6211

Browse files
committed
fix(memory): deduplicate observations within a single add_observations call
Signed-off-by: Power <[email protected]>
1 parent 82acf2a commit 23b6211

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-memory": patch
3+
---
4+
5+
Deduplicate observations within a single `add_observations` call (#5201).

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,36 @@ describe("KnowledgeGraphManager", () => {
229229
expect(alice?.observations).toHaveLength(3);
230230
});
231231

232+
it("should deduplicate observations within a single call", async () => {
233+
await manager.createEntities([
234+
{
235+
name: "Alice",
236+
entityType: "person",
237+
observations: ["works at Acme Corp"],
238+
},
239+
]);
240+
241+
const results = await manager.addObservations([
242+
{
243+
entityName: "Alice",
244+
contents: ["likes coffee", "likes coffee", "has a dog", "has a dog"],
245+
},
246+
]);
247+
248+
expect(results[0].addedObservations).toEqual([
249+
"likes coffee",
250+
"has a dog",
251+
]);
252+
253+
const graph = await manager.readGraph();
254+
const alice = graph.entities.find((e) => e.name === "Alice");
255+
expect(alice?.observations).toEqual([
256+
"works at Acme Corp",
257+
"likes coffee",
258+
"has a dog",
259+
]);
260+
});
261+
232262
it("should throw error for non-existent entity", async () => {
233263
await expect(async () => {
234264
await manager.addObservations([

‎src/memory/index.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,14 @@ export class KnowledgeGraphManager {
574574
if (!entity) {
575575
throw new Error(`Entity with name ${o.entityName} not found`);
576576
}
577-
const newObservations = o.contents.filter(
578-
(content) => !entity.observations.includes(content),
579-
);
577+
const seen = new Set(entity.observations);
578+
const newObservations: string[] = [];
579+
for (const content of o.contents) {
580+
if (!seen.has(content)) {
581+
seen.add(content);
582+
newObservations.push(content);
583+
}
584+
}
580585
entity.observations.push(...newObservations);
581586
return { entityName: o.entityName, addedObservations: newObservations };
582587
});

0 commit comments

Comments
 (0)