mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 11:01:15 +00:00
fix(editor): Handle large payloads in the AI Assistant requests better (#12747)
This commit is contained in:
committed by
GitHub
parent
60187cab9b
commit
eb4dea1ca8
@@ -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'}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user