feat: split h5 direct chat from goosed tasks

This commit is contained in:
john
2026-07-04 14:01:00 +08:00
parent eb8d760dd9
commit 29d9a87d4f
18 changed files with 874 additions and 32 deletions
+57 -2
View File
@@ -1,8 +1,9 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createConversationMemoryService, extractConversationMessageText } from './conversation-memory.mjs';
import { encryptSecret } from './llm-providers.mjs';
function createPool() {
function createPool({ provider = null } = {}) {
const state = {
messages: [],
memories: [],
@@ -101,7 +102,7 @@ function createPool() {
.map((item) => ({ ...item, status: 'active' })),
];
}
if (sql.includes('FROM h5_llm_provider_keys')) return [[]];
if (sql.includes('FROM h5_llm_provider_keys')) return [[provider].filter(Boolean)];
throw new Error(`Unexpected SQL: ${sql}`);
},
};
@@ -179,6 +180,60 @@ test('saveAndAnalyze leaves messages unanalyzed when extraction fails and no mem
else process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = previous;
});
test('saveAndAnalyze throttles repeated llm extraction warnings', async () => {
const previous = process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = '1';
const warnings = [];
const originalWarn = console.warn;
console.warn = (...args) => warnings.push(args);
let currentNow = 4000;
try {
const secret = encryptSecret('test-key', 'memory-test-key');
const pool = createPool({
provider: {
api_url: 'https://llm.example.com/v1',
default_model: 'memory-model',
api_key_ciphertext: secret.ciphertext,
api_key_iv: secret.iv,
api_key_tag: secret.tag,
},
});
const service = createConversationMemoryService(pool, {
now: () => currentNow,
encryptionKey: 'memory-test-key',
llmWarningIntervalMs: 60000,
fetch: async () => {
throw new Error('upstream unavailable');
},
});
await service.saveAndAnalyze('session-warn-1', 'user-warn', [
{
id: 'warn-1',
role: 'user',
content: [{ type: 'text', text: '今天下雨了。' }],
metadata: { userVisible: true },
},
]);
currentNow += 1000;
await service.saveAndAnalyze('session-warn-2', 'user-warn', [
{
id: 'warn-2',
role: 'user',
content: [{ type: 'text', text: '明天可能也下雨。' }],
metadata: { userVisible: true },
},
]);
} finally {
console.warn = originalWarn;
if (previous == null) delete process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED;
else process.env.USER_CONVERSATION_MEMORY_LLM_ENABLED = previous;
}
assert.equal(warnings.length, 1);
assert.match(String(warnings[0][0]), /conversation-memory/);
});
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';