|
| 1 | +export type ChunkDiscordTextOpts = { |
| 2 | + /** Max characters per Discord message. Default: 2000. */ |
| 3 | + maxChars?: number; |
| 4 | + /** |
| 5 | + * Soft max line count per message. |
| 6 | + * |
| 7 | + * Discord clients can "clip"/collapse very tall messages in the UI; splitting |
| 8 | + * by lines keeps long multi-paragraph replies readable. |
| 9 | + */ |
| 10 | + maxLines?: number; |
| 11 | +}; |
| 12 | + |
| 13 | +const DEFAULT_MAX_CHARS = 2000; |
| 14 | +const DEFAULT_MAX_LINES = 20; |
| 15 | + |
| 16 | +function countLines(text: string) { |
| 17 | + if (!text) return 0; |
| 18 | + return text.split("\n").length; |
| 19 | +} |
| 20 | + |
| 21 | +function isFenceLine(line: string) { |
| 22 | + return line.trim().startsWith("```"); |
| 23 | +} |
| 24 | + |
| 25 | +function splitLongLine(line: string, maxChars: number): string[] { |
| 26 | + if (line.length <= maxChars) return [line]; |
| 27 | + const out: string[] = []; |
| 28 | + let remaining = line; |
| 29 | + while (remaining.length > maxChars) { |
| 30 | + out.push(remaining.slice(0, maxChars)); |
| 31 | + remaining = remaining.slice(maxChars); |
| 32 | + } |
| 33 | + if (remaining.length) out.push(remaining); |
| 34 | + return out; |
| 35 | +} |
| 36 | + |
| 37 | +function closeFenceIfNeeded(text: string, fenceOpen: string | null) { |
| 38 | + if (!fenceOpen) return text; |
| 39 | + if (!text.endsWith("\n")) return `${text}\n\`\`\``; |
| 40 | + return `${text}\`\`\``; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Chunks outbound Discord text by both character count and (soft) line count, |
| 45 | + * while keeping fenced code blocks balanced across chunks. |
| 46 | + */ |
| 47 | +export function chunkDiscordText( |
| 48 | + text: string, |
| 49 | + opts: ChunkDiscordTextOpts = {}, |
| 50 | +): string[] { |
| 51 | + const maxChars = opts.maxChars ?? DEFAULT_MAX_CHARS; |
| 52 | + const maxLines = opts.maxLines ?? DEFAULT_MAX_LINES; |
| 53 | + |
| 54 | + const trimmed = text ?? ""; |
| 55 | + if (!trimmed) return []; |
| 56 | + |
| 57 | + const alreadyOk = |
| 58 | + trimmed.length <= maxChars && countLines(trimmed) <= maxLines; |
| 59 | + if (alreadyOk) return [trimmed]; |
| 60 | + |
| 61 | + const lines = trimmed.split("\n"); |
| 62 | + const chunks: string[] = []; |
| 63 | + |
| 64 | + let current = ""; |
| 65 | + let currentLines = 0; |
| 66 | + let openFence: string | null = null; |
| 67 | + |
| 68 | + const flush = () => { |
| 69 | + const payload = closeFenceIfNeeded(current, openFence); |
| 70 | + if (payload.trim().length) chunks.push(payload); |
| 71 | + current = ""; |
| 72 | + currentLines = 0; |
| 73 | + if (openFence) { |
| 74 | + current = openFence; |
| 75 | + currentLines = 1; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + for (const originalLine of lines) { |
| 80 | + if (isFenceLine(originalLine)) { |
| 81 | + openFence = openFence ? null : originalLine; |
| 82 | + } |
| 83 | + |
| 84 | + const segments = splitLongLine(originalLine, maxChars); |
| 85 | + for (let segIndex = 0; segIndex < segments.length; segIndex++) { |
| 86 | + const segment = segments[segIndex]; |
| 87 | + const isLineContinuation = segIndex > 0; |
| 88 | + const delimiter = isLineContinuation |
| 89 | + ? "" |
| 90 | + : current.length > 0 |
| 91 | + ? "\n" |
| 92 | + : ""; |
| 93 | + const addition = `${delimiter}${segment}`; |
| 94 | + const nextLen = current.length + addition.length; |
| 95 | + const nextLines = currentLines + (isLineContinuation ? 0 : 1); |
| 96 | + |
| 97 | + const wouldExceedChars = nextLen > maxChars; |
| 98 | + const wouldExceedLines = nextLines > maxLines; |
| 99 | + |
| 100 | + if ((wouldExceedChars || wouldExceedLines) && current.length > 0) { |
| 101 | + flush(); |
| 102 | + } |
| 103 | + |
| 104 | + if (current.length > 0) { |
| 105 | + current += addition; |
| 106 | + if (!isLineContinuation) currentLines += 1; |
| 107 | + } else { |
| 108 | + current = segment; |
| 109 | + currentLines = 1; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (current.length) { |
| 115 | + const payload = closeFenceIfNeeded(current, openFence); |
| 116 | + if (payload.trim().length) chunks.push(payload); |
| 117 | + } |
| 118 | + |
| 119 | + return chunks; |
| 120 | +} |
0 commit comments