fix(editor): Handle large payloads in the AI Assistant requests better (#12747)

This commit is contained in:
Milorad FIlipović
2025-01-22 14:50:28 +01:00
committed by GitHub
parent 60187cab9b
commit eb4dea1ca8
12 changed files with 751 additions and 24 deletions

View File

@@ -18,3 +18,35 @@ export const searchInObject = (obj: ObjectOrArray, searchString: string): boolea
? searchInObject(entry, searchString)
: entry?.toString().toLowerCase().includes(searchString.toLowerCase()),
);
/**
* Calculate the size of a stringified object in KB.
* @param {unknown} obj - The object to calculate the size of
* @returns {number} The size of the object in KB
* @throws {Error} If the object is not serializable
*/
export const getObjectSizeInKB = (obj: unknown): number => {
if (obj === null || obj === undefined) {
return 0;
}
if (
(typeof obj === 'object' && Object.keys(obj).length === 0) ||
(Array.isArray(obj) && obj.length === 0)
) {
// "{}" and "[]" both take 2 bytes in UTF-8
return Number((2 / 1024).toFixed(2));
}
try {
const str = JSON.stringify(obj);
// Using TextEncoder to get actual UTF-8 byte length (what we see in chrome dev tools)
const bytes = new TextEncoder().encode(str).length;
const kb = bytes / 1024;
return Number(kb.toFixed(2));
} catch (error) {
throw new Error(
`Failed to calculate object size: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
};