1+ import { resolveIntegerOption } from "openclaw/plugin-sdk/number-runtime" ;
12import { chunkMarkdownTextWithMode , type ChunkMode } from "openclaw/plugin-sdk/reply-chunking" ;
23
34type ChunkDiscordTextOpts = {
@@ -24,6 +25,10 @@ const DEFAULT_MAX_LINES = 17;
2425const FENCE_RE = / ^ ( { 0 , 3 } ) ( ` { 3 , } | ~ { 3 , } ) ( .* ) $ / ;
2526const CJK_PUNCTUATION_BREAK_AFTER_RE = / [ 、 。 , . ! ? ; : ) ] } 〉 》 」 』 】 〕 〗 〙 ] / u;
2627
28+ function resolveDiscordChunkLimit ( value : unknown , fallback : number ) {
29+ return resolveIntegerOption ( value , fallback , { min : 1 } ) ;
30+ }
31+
2732function countLines ( text : string ) {
2833 if ( ! text ) {
2934 return 0 ;
@@ -114,7 +119,7 @@ function splitLongLine(
114119 maxChars : number ,
115120 opts : { preserveWhitespace : boolean } ,
116121) : string [ ] {
117- const limit = Math . max ( 1 , Math . floor ( maxChars ) ) ;
122+ const limit = resolveDiscordChunkLimit ( maxChars , DEFAULT_MAX_CHARS ) ;
118123 if ( line . length <= limit ) {
119124 return [ line ] ;
120125 }
@@ -150,8 +155,8 @@ function splitLongLine(
150155 * while keeping fenced code blocks balanced across chunks.
151156 */
152157export 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 ) ;
155160
156161 const body = text ?? "" ;
157162 if ( ! body ) {
@@ -262,7 +267,7 @@ export function chunkDiscordTextWithMode(
262267 }
263268 const lineChunks = chunkMarkdownTextWithMode (
264269 text ,
265- Math . max ( 1 , Math . floor ( opts . maxChars ?? DEFAULT_MAX_CHARS ) ) ,
270+ resolveDiscordChunkLimit ( opts . maxChars , DEFAULT_MAX_CHARS ) ,
266271 "newline" ,
267272 ) ;
268273 const chunks : string [ ] = [ ] ;
0 commit comments