Skip to content

Commit f15f235

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

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
@@ -633,9 +633,14 @@ export class KnowledgeGraphManager {
633633
if (!entity) {
634634
throw new Error(`Entity with name ${o.entityName} not found`);
635635
}
636-
const newObservations = o.contents.filter(
637-
(content) => !entity.observations.includes(content),
638-
);
636+
const seen = new Set(entity.observations);
637+
const newObservations: string[] = [];
638+
for (const content of o.contents) {
639+
if (!seen.has(content)) {
640+
seen.add(content);
641+
newObservations.push(content);
642+
}
643+
}
639644
entity.observations.push(...newObservations);
640645
return { entityName: o.entityName, addedObservations: newObservations };
641646
});

0 commit comments

Comments
 (0)