InMemoryConversationStore¶
InMemoryConversationStore is a Map-based implementation of ConversationStore for tests and local development. It has zero external dependencies and provides helper methods for test assertions.
Import¶
Constructor¶
No configuration required.
Methods¶
Implements all methods from the ConversationStore interface, plus the following test helpers:
Test Helpers¶
| Method | Signature | Description |
|---|---|---|
getAllConversations | getAllConversations(): Conversation[] | Returns all conversations (unfiltered). Useful in tests. |
getAllMessages | getAllMessages(): ConversationMessage[] | Returns all messages across all conversations. Useful in tests. |
clear | clear(): void | Clears all conversations and messages. Call between test cases. |
Code Example¶
import { InMemoryConversationStore } from "@modernpath/agent-framework";
const store = new InMemoryConversationStore();
// Create a conversation
await store.createConversation({
schemaVersion: 1,
conversationId: "conv-001",
userId: 1,
auditingId: 100,
agentName: "QAChatAgent",
title: "Building Temperature Issue",
createdAt: new Date(),
updatedAt: new Date(),
});
// Add messages
await store.addMessage("conv-001", {
schemaVersion: 1,
messageId: "msg-001",
conversationId: "conv-001",
role: "user",
content: "Why is the building cold?",
timestamp: new Date(),
});
await store.addMessage("conv-001", {
schemaVersion: 1,
messageId: "msg-002",
conversationId: "conv-001",
role: "assistant",
content: "Based on the telemetry data, the heating valve is stuck at 20%.",
timestamp: new Date(),
traceId: "trace-abc",
sources: [{ documentName: "heating-guide.md", score: 0.92 }],
});
// Retrieve messages (most recent 50, ascending order)
const messages = await store.getMessages("conv-001", { limit: 50 });
console.log(messages.length); // 2
// List conversations for a user
const conversations = await store.listConversations({ userId: 1 });
console.log(conversations[0].messageCount); // 2
// Update conversation title
await store.updateConversation("conv-001", { title: "Heating Valve Issue" });
Test Example¶
import { describe, it, expect, beforeEach } from "vitest";
import { InMemoryConversationStore } from "@modernpath/agent-framework";
describe("Conversation handler", () => {
let store: InMemoryConversationStore;
beforeEach(() => {
store = new InMemoryConversationStore();
});
it("should create conversation and add messages", async () => {
await store.createConversation({
schemaVersion: 1,
conversationId: "test-conv",
userId: 1,
auditingId: 1,
agentName: "TestAgent",
createdAt: new Date(),
updatedAt: new Date(),
});
await store.addMessage("test-conv", {
schemaVersion: 1,
messageId: "msg-1",
conversationId: "test-conv",
role: "user",
content: "Hello",
timestamp: new Date(),
});
const conv = await store.getConversation("test-conv");
expect(conv?.messageCount).toBe(1);
const messages = store.getAllMessages();
expect(messages).toHaveLength(1);
expect(messages[0].role).toBe("user");
});
});
Related Pages¶
- ConversationStore Interface -- the interface this class implements
- FirestoreConversationStore -- production implementation
- Tracing / InMemoryTraceStore -- similar pattern for trace testing