|
| 1 | +import { resolveIntegerOption } from "openclaw/plugin-sdk/number-runtime"; |
1 | 2 | import { chunkMarkdownTextWithMode, type ChunkMode } from "openclaw/plugin-sdk/reply-chunking"; |
2 | 3 | |
3 | 4 | type ChunkDiscordTextOpts = { |
@@ -24,6 +25,10 @@ const DEFAULT_MAX_LINES = 17;
|
24 | 25 | const FENCE_RE = /^( {0,3})(`{3,}|~{3,})(.*)$/; |
25 | 26 | const CJK_PUNCTUATION_BREAK_AFTER_RE = /[、。,.!?;:)]}〉》」』】〕〗〙]/u; |
26 | 27 | |
| 28 | +function resolveDiscordChunkLimit(value: unknown, fallback: number) { |
| 29 | +return resolveIntegerOption(value, fallback, { min: 1 }); |
| 30 | +} |
| 31 | + |
27 | 32 | function countLines(text: string) { |
28 | 33 | if (!text) { |
29 | 34 | return 0; |
@@ -114,7 +119,7 @@ function splitLongLine(
|
114 | 119 | maxChars: number, |
115 | 120 | opts: { preserveWhitespace: boolean }, |
116 | 121 | ): string[] { |
117 | | -const limit = Math.max(1, Math.floor(maxChars)); |
| 122 | +const limit = resolveDiscordChunkLimit(maxChars, DEFAULT_MAX_CHARS); |
118 | 123 | if (line.length <= limit) { |
119 | 124 | return [line]; |
120 | 125 | } |
@@ -150,8 +155,8 @@ function splitLongLine(
|
150 | 155 | * while keeping fenced code blocks balanced across chunks. |
151 | 156 | */ |
152 | 157 | export function chunkDiscordText(text: string, opts: ChunkDiscordTextOpts = {}): string[] { |
153 | | -const maxChars = Math.max(1, Math.floor(opts.maxChars ?? DEFAULT_MAX_CHARS)); |
154 | | -const maxLines = Math.max(1, Math.floor(opts.maxLines ?? DEFAULT_MAX_LINES)); |
| 158 | +const maxChars = resolveDiscordChunkLimit(opts.maxChars, DEFAULT_MAX_CHARS); |
| 159 | +const maxLines = resolveDiscordChunkLimit(opts.maxLines, DEFAULT_MAX_LINES); |
155 | 160 | |
156 | 161 | const body = text ?? ""; |
157 | 162 | if (!body) { |
@@ -262,7 +267,7 @@ export function chunkDiscordTextWithMode(
|
262 | 267 | } |
263 | 268 | const lineChunks = chunkMarkdownTextWithMode( |
264 | 269 | text, |
265 | | -Math.max(1, Math.floor(opts.maxChars ?? DEFAULT_MAX_CHARS)), |
| 270 | +resolveDiscordChunkLimit(opts.maxChars, DEFAULT_MAX_CHARS), |
266 | 271 | "newline", |
267 | 272 | ); |
268 | 273 | const chunks: string[] = []; |
|