fix: harden conversation memory extraction flow
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import crypto from 'node:crypto';
|
||||
import { Agent, fetch as undiciFetch } from 'undici';
|
||||
import { fetch as undiciFetch } from 'undici';
|
||||
import { jsonrepair } from 'jsonrepair';
|
||||
import {
|
||||
decryptSecret,
|
||||
@@ -7,10 +7,6 @@ import {
|
||||
resolveChatCompletionsUrl,
|
||||
} from './llm-providers.mjs';
|
||||
|
||||
const insecureDispatcher = new Agent({
|
||||
connect: { rejectUnauthorized: false },
|
||||
});
|
||||
|
||||
const MAX_MESSAGE_TEXT = 12000;
|
||||
const MAX_ANALYSIS_MESSAGES = 8;
|
||||
const MAX_MEMORY_TEXT = 1000;
|
||||
@@ -264,7 +260,6 @@ export function createConversationMemoryService(pool, options = {}) {
|
||||
temperature: 0,
|
||||
...(row.relay_provider ? { provider: row.relay_provider } : {}),
|
||||
}),
|
||||
dispatcher: url.startsWith('https://') ? insecureDispatcher : undefined,
|
||||
});
|
||||
const text = await upstream.text().catch(() => '');
|
||||
if (!upstream.ok) return null;
|
||||
@@ -317,16 +312,20 @@ export function createConversationMemoryService(pool, options = {}) {
|
||||
const messages = await loadUnanalyzedUserMessages(userId);
|
||||
if (!messages.length) return { ok: true, analyzed: 0, memories: 0 };
|
||||
let memories = null;
|
||||
let extractionSucceeded = false;
|
||||
if (llmEnabled()) {
|
||||
try {
|
||||
memories = await extractWithLlm(messages);
|
||||
extractionSucceeded = Array.isArray(memories);
|
||||
} catch (err) {
|
||||
console.warn('[conversation-memory] LLM extraction failed:', err instanceof Error ? err.message : err);
|
||||
}
|
||||
}
|
||||
if (!memories) memories = fallbackMemoriesFromMessages(messages);
|
||||
const stored = await storeMemories(userId, messages, memories);
|
||||
await markAnalyzed(messages.map((message) => message.id));
|
||||
if (!llmEnabled() || extractionSucceeded || stored > 0) {
|
||||
await markAnalyzed(messages.map((message) => message.id));
|
||||
}
|
||||
return { ok: true, analyzed: messages.length, memories: stored };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user