fix: harden conversation memory extraction flow
This commit is contained in:
@@ -150,3 +150,54 @@ test('saveAndAnalyze stores messages and fallback memories', async () => {
|
||||
if (previous == null) delete process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
|
||||
else process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = previous;
|
||||
});
|
||||
|
||||
test('saveAndAnalyze leaves messages unanalyzed when extraction fails and no memory is stored', async () => {
|
||||
const previous = process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
|
||||
process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = '1';
|
||||
const pool = createPool();
|
||||
const service = createConversationMemoryService(pool, {
|
||||
now: () => 2000,
|
||||
fetch: async () => {
|
||||
throw new Error('upstream unavailable');
|
||||
},
|
||||
});
|
||||
|
||||
const result = await service.saveAndAnalyze('session-2', 'user-2', [
|
||||
{
|
||||
id: 'm3',
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: '今天下雨了。' }],
|
||||
metadata: { userVisible: true },
|
||||
},
|
||||
]);
|
||||
|
||||
assert.equal(result.saved, 1);
|
||||
assert.equal(result.memories, 0);
|
||||
assert.equal(pool.state.messages.find((item) => item.message_key === 'm3')?.analyzed_at, null);
|
||||
|
||||
if (previous == null) delete process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
|
||||
else process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = previous;
|
||||
});
|
||||
|
||||
test('saveAndAnalyze still marks messages analyzed when llm extraction is disabled and fallback stores nothing', async () => {
|
||||
const previous = process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
|
||||
process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = '0';
|
||||
const pool = createPool();
|
||||
const service = createConversationMemoryService(pool, { now: () => 3000 });
|
||||
|
||||
const result = await service.saveAndAnalyze('session-3', 'user-3', [
|
||||
{
|
||||
id: 'm4',
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: '今天天气不错。' }],
|
||||
metadata: { userVisible: true },
|
||||
},
|
||||
]);
|
||||
|
||||
assert.equal(result.saved, 1);
|
||||
assert.equal(result.memories, 0);
|
||||
assert.equal(pool.state.messages.find((item) => item.message_key === 'm4')?.analyzed_at, 3000);
|
||||
|
||||
if (previous == null) delete process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
|
||||
else process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = previous;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user