114 lines
4.3 KiB
TypeScript
114 lines
4.3 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
createInMemoryMemoryStore,
|
|
PERSONAL_SCOPE,
|
|
type MemoryStore,
|
|
type RememberInput,
|
|
} from '@waggle/memory-contract';
|
|
import { MemoryExtractionQueue } from '../../src/local/memory-extraction.js';
|
|
|
|
/** An in-memory store that claims extraction and records the conversations it gets. */
|
|
function extractingStore(): { store: MemoryStore; calls: RememberInput[] } {
|
|
const inner = createInMemoryMemoryStore();
|
|
const calls: RememberInput[] = [];
|
|
const store: MemoryStore = {
|
|
...inner,
|
|
capabilities: { ...inner.capabilities, extraction: true },
|
|
remember: async (input) => {
|
|
calls.push(input);
|
|
return [];
|
|
},
|
|
};
|
|
return { store, calls };
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe('MemoryExtractionQueue', () => {
|
|
it('extracts a batch once enough messages are queued', async () => {
|
|
const { store, calls } = extractingStore();
|
|
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 4 });
|
|
queue.record('s1', 'I moved to Novi Sad.', 'Noted.');
|
|
expect(calls).toHaveLength(0);
|
|
queue.record('s1', 'I work as a designer.', 'Got it.');
|
|
await queue.flush('s1');
|
|
expect(calls).toHaveLength(1);
|
|
expect(calls[0]).toMatchObject({
|
|
scope: PERSONAL_SCOPE,
|
|
kind: 'profile',
|
|
origin: 'extraction',
|
|
sessionId: 's1',
|
|
conversation: [
|
|
{ role: 'user', content: 'I moved to Novi Sad.' },
|
|
{ role: 'assistant', content: 'Noted.' },
|
|
{ role: 'user', content: 'I work as a designer.' },
|
|
{ role: 'assistant', content: 'Got it.' },
|
|
],
|
|
});
|
|
queue.stop();
|
|
});
|
|
|
|
it('extracts pending messages after a pause', async () => {
|
|
vi.useFakeTimers();
|
|
const { store, calls } = extractingStore();
|
|
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 10, idleMs: 1000 });
|
|
queue.record('s1', 'My daughter is called Ana.', 'Lovely name.');
|
|
await vi.advanceTimersByTimeAsync(999);
|
|
expect(calls).toHaveLength(0);
|
|
await vi.advanceTimersByTimeAsync(1);
|
|
await queue.flush();
|
|
expect(calls).toHaveLength(1);
|
|
queue.stop();
|
|
});
|
|
|
|
it('keeps sessions apart', async () => {
|
|
const { store, calls } = extractingStore();
|
|
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 10 });
|
|
queue.record('a', 'I like tea.', 'OK.');
|
|
queue.record('b', 'I like coffee.', 'OK.');
|
|
await queue.flush();
|
|
expect(calls.map((c) => c.sessionId).sort()).toEqual(['a', 'b']);
|
|
queue.stop();
|
|
});
|
|
|
|
it('collects nothing when the setting is off or the store cannot extract', async () => {
|
|
const { store, calls } = extractingStore();
|
|
let enabled = false;
|
|
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => enabled, batchMessages: 2 });
|
|
queue.record('s1', 'I live in Belgrade.', 'OK.');
|
|
await queue.flush();
|
|
expect(calls).toHaveLength(0);
|
|
|
|
enabled = true;
|
|
const plain = new MemoryExtractionQueue({ memory: createInMemoryMemoryStore(), isEnabled: () => true, batchMessages: 2 });
|
|
expect(plain.active).toBe(false);
|
|
queue.stop();
|
|
plain.stop();
|
|
});
|
|
|
|
it('reports a failed extraction without throwing', async () => {
|
|
const { store } = extractingStore();
|
|
const failing: MemoryStore = { ...store, remember: async () => { throw new Error('model unreachable'); } };
|
|
const onError = vi.fn();
|
|
const queue = new MemoryExtractionQueue({ memory: failing, isEnabled: () => true, batchMessages: 2, onError });
|
|
queue.record('s1', 'I live in Belgrade.', 'OK.');
|
|
await queue.flush();
|
|
expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'model unreachable' }));
|
|
queue.stop();
|
|
});
|
|
|
|
it('skips batches without a user message and trims long replies', async () => {
|
|
const { store, calls } = extractingStore();
|
|
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 2 });
|
|
queue.record('s1', ' ', 'x'.repeat(10_000));
|
|
await queue.flush();
|
|
expect(calls).toHaveLength(0);
|
|
queue.record('s2', 'Hi, I am Mila.', 'y'.repeat(10_000));
|
|
await queue.flush();
|
|
expect(calls[0]?.conversation?.[1]?.content).toHaveLength(4000);
|
|
queue.stop();
|
|
});
|
|
});
|