fix: prevent consolidation/reformat truncation on large memory sets

- Consolidation: increase max_tokens from responseLength*2 to
  Math.max(responseLength*4, 4000) — ensures at least 4k output tokens
  regardless of the user's Response Length setting
- Reformat/Convert: increase from responseLength to
  Math.max(responseLength*2, 4000) — same floor
- Add truncation detection in consolidation: warns in activity log and
  toastr if response contains <memory> tags but doesn't end with
  </memory>, pointing user to Response Length setting

Fixes #13

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
bal-spec 2026-03-30 21:30:39 -07:00
parent 2fc514c0b2
commit 700a1b4dc5

View file

@ -672,7 +672,7 @@ async function convertWithLLM(content, charName) {
let response;
try {
response = await callLLM(prompt, extension_settings[MODULE_NAME].responseLength || 2000, 'You are a text restructuring assistant. Preserve all information faithfully.');
response = await callLLM(prompt, Math.max(extension_settings[MODULE_NAME].responseLength * 2 || 4000, 4000), 'You are a text restructuring assistant. Preserve all information faithfully.');
} catch (err) {
console.error(LOG_PREFIX, 'LLM conversion failed:', err);
return { blocks: [], warnings: [`LLM call failed: ${err.message || 'Unknown error'}`] };
@ -7013,7 +7013,7 @@ async function runConsolidationLLM(memories, charName) {
const llmStartTime = Date.now();
const result = await callLLM(
prompt,
extension_settings[MODULE_NAME].responseLength * 2,
Math.max(extension_settings[MODULE_NAME].responseLength * 4, 4000),
'You are a memory consolidation assistant.',
);
@ -7032,6 +7032,12 @@ async function runConsolidationLLM(memories, charName) {
return null;
}
// Detect truncated response — a complete response ends with </memory>
if (cleanResult.includes('<memory') && !/<\/memory>\s*$/.test(cleanResult)) {
logActivity('Consolidation response appears truncated — last block(s) may be missing. Try increasing Response Length in Settings → Extraction.', 'warning');
toastr.warning(t`Consolidation response may be truncated. Try increasing Response Length in Settings → Extraction.`, 'CharMemory', { timeOut: 8000 });
}
// Parse into memory format, then serialize back to plain text for the editor
const timestamp = getTimestamp();