From 8ccdad18f470f9a7dd2a3e8dfbede54d05641c47 Mon Sep 17 00:00:00 2001 From: seb Date: Wed, 14 Oct 2026 01:14:02 +0200 Subject: [PATCH] u --- src/services/draftManager.js | 41 ++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/services/draftManager.js b/src/services/draftManager.js index 7e1e0ed..9940997 100644 --- a/src/services/draftManager.js +++ b/src/services/draftManager.js @@ -50,11 +50,43 @@ export function getCachedDraft({ companyId, userId, name }) { return draftCache.get(cacheKey(companyId, userId, name)) || null; } +/** + * Normalizes draft content by deduplicating redundant `items` array when `lines` exists. + * @param {any} content + * @returns {any} + */ +export function normalizeDraftContent(content) { + if (!content) return content; + let parsed = content; + let wasString = false; + if (typeof content === 'string') { + try { + parsed = JSON.parse(content); + wasString = true; + } catch { + return content; + } + } + if (parsed && typeof parsed === 'object') { + const target = parsed.manifest && typeof parsed.manifest === 'object' ? parsed.manifest : parsed; + if (target && typeof target === 'object') { + if (Array.isArray(target.lines) && Array.isArray(target.items)) { + delete target.items; + } else if (!Array.isArray(target.lines) && Array.isArray(target.items)) { + target.lines = target.items; + delete target.items; + } + } + } + return wasString ? JSON.stringify(parsed, null, 2) : parsed; +} + export function setCachedDraft({ companyId, userId, name, content }) { + const normalized = normalizeDraftContent(content); const updatedAt = new Date().toISOString(); - const str = typeof content === 'string' ? content : JSON.stringify(content ?? {}, null, 2); + const str = typeof normalized === 'string' ? normalized : JSON.stringify(normalized ?? {}, null, 2); const entry = { - content, + content: normalized, updatedAt, size: Buffer.byteLength(str, 'utf8'), }; @@ -262,13 +294,14 @@ export function handleDraftResponse({ requestId, content, error, files }) { } // action === 'read' + const normalized = normalizeDraftContent(content); setCachedDraft({ companyId: pending.companyId, userId: pending.userId, name: pending.name, - content, + content: normalized, }); - pending.resolve(content); + pending.resolve(normalized); return { handled: true }; }