/** * Split text into chunks that respect a maximum length, avoiding * splits in the middle of Unicode surrogate pairs. */ export function splitText(text: string, maxLength: number): string[] { if (text.length <= maxLength) { return [text]; } const chunks: string[] = []; let cursor = 0; while (cursor < text.length) { let end = Math.min(cursor + maxLength, text.length); // Avoid splitting a surrogate pair if ( end < text.length && text.charCodeAt(end - 1) >= 0xd800 && text.charCodeAt(end - 1) <= 0xdbff ) { end--; } chunks.push(text.slice(cursor, end)); cursor = end; } return chunks; }