@waggle/memory-contract defines MemoryStore: remember (text, or a conversation to extract facts from), search, list, count, get, update, forget, forgetMatching and history over personal and workspace scopes, with kinds, importance, source, origin and session in place of the old content prefixes and named sessions. It ships an in-memory store and a compatibility suite that every implementation runs. @waggle/memory-mem0 implements the contract on mem0ai 3.3.1 in-process: SQLite files in one directory, scope as mem0's agent_id, Waggle's own embedder through mem0's langchain provider, fact extraction through any OpenAI-compatible endpoint. mem0's telemetry and remote notice download are switched off before the module loads; a test asserts that no request is made. Memory survives a restart (tested). New dependencies: mem0ai 3.3.1 (Apache-2.0, pinned exactly) with its required peers compromise (MIT) and @cloudflare/workers-types (MIT OR Apache-2.0); transitive openai (Apache-2.0), axios (MIT) and others under MIT or BSD-2-Clause.
129 lines
5.2 KiB
TypeScript
129 lines
5.2 KiB
TypeScript
import fs from 'node:fs';
|
|
import http from 'node:http';
|
|
import type { AddressInfo } from 'node:net';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { PERSONAL_SCOPE } from '@waggle/memory-contract';
|
|
import { createMem0MemoryStore, type TextEmbedder } from '../src/index.js';
|
|
import { describeMemoryStoreContract } from '../../memory-contract/tests/contract-suite.js';
|
|
|
|
/** Deterministic bag-of-words embedder: texts that share words get similar vectors. */
|
|
const wordEmbedder: TextEmbedder = {
|
|
dimensions: 256,
|
|
async embed(text) {
|
|
const vector = new Array<number>(256).fill(0);
|
|
for (const word of text.toLowerCase().match(/[\p{L}\p{N}]{2,}/gu) ?? []) {
|
|
let hash = 2166136261;
|
|
for (const char of word) hash = Math.imul(hash ^ char.charCodeAt(0), 16777619);
|
|
vector[Math.abs(hash) % 256] += 1;
|
|
}
|
|
const norm = Math.hypot(...vector) || 1;
|
|
return vector.map((value) => value / norm);
|
|
},
|
|
async embedBatch(texts) {
|
|
return Promise.all(texts.map((text) => this.embed(text)));
|
|
},
|
|
};
|
|
|
|
const tempDirs: string[] = [];
|
|
function tempDir(): string {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-mem0-'));
|
|
tempDirs.push(dir);
|
|
return dir;
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describeMemoryStoreContract('mem0 store', () =>
|
|
createMem0MemoryStore({ directory: tempDir(), embedder: wordEmbedder }));
|
|
|
|
describe('mem0 store — local behaviour', () => {
|
|
it('keeps memories across a restart', async () => {
|
|
const directory = tempDir();
|
|
const first = await createMem0MemoryStore({ directory, embedder: wordEmbedder });
|
|
const [change] = await first.remember({ scope: PERSONAL_SCOPE, text: 'My sister lives in Zagreb' });
|
|
await first.close();
|
|
|
|
const second = await createMem0MemoryStore({ directory, embedder: wordEmbedder });
|
|
expect(await second.get(change.record.id)).toMatchObject({ text: 'My sister lives in Zagreb' });
|
|
expect(await second.count({ scopes: [PERSONAL_SCOPE] })).toBe(1);
|
|
await second.close();
|
|
expect(fs.existsSync(path.join(directory, 'vectors.db'))).toBe(true);
|
|
expect(fs.existsSync(path.join(directory, 'history.db'))).toBe(true);
|
|
});
|
|
|
|
it('sends nothing to mem0 telemetry and stays out of ~/.mem0', async () => {
|
|
const fetchSpy = vi.spyOn(globalThis, 'fetch');
|
|
const store = await createMem0MemoryStore({ directory: tempDir(), embedder: wordEmbedder });
|
|
const [change] = await store.remember({ scope: PERSONAL_SCOPE, text: 'Prefers morning meetings' });
|
|
await store.search('meetings', { scopes: [PERSONAL_SCOPE] });
|
|
await store.list({ scopes: [PERSONAL_SCOPE] });
|
|
await store.update(change.record.id, { importance: 'important' });
|
|
await store.forget(change.record.id);
|
|
await store.close();
|
|
expect(fetchSpy).not.toHaveBeenCalled();
|
|
expect(fs.existsSync(path.join(os.homedir(), '.mem0'))).toBe(false);
|
|
});
|
|
|
|
it('offers extraction only when a model is configured', async () => {
|
|
const store = await createMem0MemoryStore({ directory: tempDir(), embedder: wordEmbedder });
|
|
expect(store.capabilities).toMatchObject({ extraction: false, semanticSearch: true, history: true });
|
|
await store.close();
|
|
});
|
|
|
|
it('extracts facts from a conversation with the configured model', async () => {
|
|
const requests: Array<{ model: string; response_format?: { type: string } }> = [];
|
|
const server = http.createServer((req, res) => {
|
|
let body = '';
|
|
req.on('data', (chunk) => { body += chunk; });
|
|
req.on('end', () => {
|
|
requests.push(JSON.parse(body));
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.end(JSON.stringify({
|
|
id: 'chatcmpl-test',
|
|
object: 'chat.completion',
|
|
created: 0,
|
|
model: 'test-model',
|
|
choices: [{
|
|
index: 0,
|
|
finish_reason: 'stop',
|
|
message: { role: 'assistant', content: JSON.stringify({ memory: [{ text: 'Lives in Novi Sad' }] }) },
|
|
}],
|
|
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
|
|
}));
|
|
});
|
|
});
|
|
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
const { port } = server.address() as AddressInfo;
|
|
try {
|
|
const store = await createMem0MemoryStore({
|
|
directory: tempDir(),
|
|
embedder: wordEmbedder,
|
|
extractionModel: { baseURL: `http://127.0.0.1:${port}/v1`, apiKey: 'test-key', model: 'test-model' },
|
|
});
|
|
expect(store.capabilities.extraction).toBe(true);
|
|
const changes = await store.remember({
|
|
scope: PERSONAL_SCOPE,
|
|
conversation: [
|
|
{ role: 'user', content: 'I moved to Novi Sad last year.' },
|
|
{ role: 'assistant', content: 'Noted.' },
|
|
],
|
|
kind: 'profile',
|
|
origin: 'chat',
|
|
});
|
|
expect(changes).toHaveLength(1);
|
|
expect(changes[0]).toMatchObject({
|
|
event: 'added',
|
|
record: { text: 'Lives in Novi Sad', scope: PERSONAL_SCOPE, kind: 'profile', origin: 'chat' },
|
|
});
|
|
expect(requests[0]).toMatchObject({ model: 'test-model', response_format: { type: 'json_object' } });
|
|
await store.close();
|
|
} finally {
|
|
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
}
|
|
});
|
|
});
|