u
This commit is contained in:
@@ -50,11 +50,43 @@ export function getCachedDraft({ companyId, userId, name }) {
|
|||||||
return draftCache.get(cacheKey(companyId, userId, name)) || null;
|
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 }) {
|
export function setCachedDraft({ companyId, userId, name, content }) {
|
||||||
|
const normalized = normalizeDraftContent(content);
|
||||||
const updatedAt = new Date().toISOString();
|
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 = {
|
const entry = {
|
||||||
content,
|
content: normalized,
|
||||||
updatedAt,
|
updatedAt,
|
||||||
size: Buffer.byteLength(str, 'utf8'),
|
size: Buffer.byteLength(str, 'utf8'),
|
||||||
};
|
};
|
||||||
@@ -262,13 +294,14 @@ export function handleDraftResponse({ requestId, content, error, files }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// action === 'read'
|
// action === 'read'
|
||||||
|
const normalized = normalizeDraftContent(content);
|
||||||
setCachedDraft({
|
setCachedDraft({
|
||||||
companyId: pending.companyId,
|
companyId: pending.companyId,
|
||||||
userId: pending.userId,
|
userId: pending.userId,
|
||||||
name: pending.name,
|
name: pending.name,
|
||||||
content,
|
content: normalized,
|
||||||
});
|
});
|
||||||
pending.resolve(content);
|
pending.resolve(normalized);
|
||||||
return { handled: true };
|
return { handled: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user