Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/yummy-actors-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/core": patch
---

Fix issue where vector embeddings were not deleted when clearing messages when using a vector adapter
Comment thread
sujal12344 marked this conversation as resolved.
18 changes: 18 additions & 0 deletions packages/core/src/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,30 @@ export class Memory {

/**
* Clear messages for a user
* Also purges vector embeddings when a vector adapter is configured,
* preventing orphaned embeddings from surfacing in future semantic searches.
*/
async clearMessages(
userId: string,
conversationId?: string,
context?: OperationContext,
): Promise<void> {
// If vector adapter is configured and a conversationId is given, clean up vectors first
if (this.vector && conversationId) {
Comment thread
sujal12344 marked this conversation as resolved.
try {
const messages = await this.storage.getMessages(userId, conversationId);
Comment thread
sujal12344 marked this conversation as resolved.
if (messages.length > 0) {
const vectorIds = messages.map((msg) => `msg_${conversationId}_${msg.id}`);
await this.vector.deleteBatch(vectorIds);
}
Comment thread
sujal12344 marked this conversation as resolved.
} catch (error) {
console.warn(
`Failed to delete vectors when clearing messages for conversation ${conversationId}:`,
error,
Comment thread
sujal12344 marked this conversation as resolved.
);
}
}

return this.storage.clearMessages(userId, conversationId, context);
Comment thread
sujal12344 marked this conversation as resolved.
Comment thread
sujal12344 marked this conversation as resolved.
}

Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/memory/semantic-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,5 +455,48 @@ describe("Memory V2 - Semantic Search", () => {
expect.arrayContaining([`msg_${conversationId}_msg1`, `msg_${conversationId}_msg2`]),
);
});

it("should delete vectors when clearMessages is called with a conversationId", async () => {
const userId = "user123";
const conversationId = "conv-clear-test";

// Create conversation and add messages
await memory.createConversation({
id: conversationId,
userId,
resourceId: "agent1",
title: "Clear Test",
metadata: {},
});

const messages: UIMessage[] = [
{
id: "msg-a",
role: "user",
parts: [{ type: "text", text: "Remember my name is Sujal" }],
},
{
id: "msg-b",
role: "assistant",
parts: [{ type: "text", text: "Got it, Sujal!" }],
},
];

await memory.addMessages(messages, userId, conversationId);

const deleteBatchSpy = vi.spyOn(vector, "deleteBatch");

// Clear messages (should also clean up vectors)
await memory.clearMessages(userId, conversationId);

// Verify vectors were deleted
expect(deleteBatchSpy).toHaveBeenCalledWith(
expect.arrayContaining([`msg_${conversationId}_msg-a`, `msg_${conversationId}_msg-b`]),
);

// Verify SQL messages are also gone
Comment thread
sujal12344 marked this conversation as resolved.
const remaining = await memory.getMessages(userId, conversationId);
expect(remaining).toHaveLength(0);
});
});
});
Loading