mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:58:26 +00:00
fix: newline chunking across channels
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { chunkMarkdownTextWithMode, type ChunkMode } from "../auto-reply/chunk.js";
|
||||
|
||||
export type ChunkDiscordTextOpts = {
|
||||
/** Max characters per Discord message. Default: 2000. */
|
||||
maxChars?: number;
|
||||
@@ -178,6 +180,31 @@ export function chunkDiscordText(text: string, opts: ChunkDiscordTextOpts = {}):
|
||||
return rebalanceReasoningItalics(text, chunks);
|
||||
}
|
||||
|
||||
export function chunkDiscordTextWithMode(
|
||||
text: string,
|
||||
opts: ChunkDiscordTextOpts & { chunkMode?: ChunkMode },
|
||||
): string[] {
|
||||
const chunkMode = opts.chunkMode ?? "length";
|
||||
if (chunkMode !== "newline") {
|
||||
return chunkDiscordText(text, opts);
|
||||
}
|
||||
const lineChunks = chunkMarkdownTextWithMode(
|
||||
text,
|
||||
Math.max(1, Math.floor(opts.maxChars ?? DEFAULT_MAX_CHARS)),
|
||||
"newline",
|
||||
);
|
||||
const chunks: string[] = [];
|
||||
for (const line of lineChunks) {
|
||||
const nested = chunkDiscordText(line, opts);
|
||||
if (!nested.length && line) {
|
||||
chunks.push(line);
|
||||
continue;
|
||||
}
|
||||
chunks.push(...nested);
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// Keep italics intact for reasoning payloads that are wrapped once with `_…_`.
|
||||
// When Discord chunking splits the message, we close italics at the end of
|
||||
// each chunk and reopen at the start of the next so every chunk renders
|
||||
|
||||
Reference in New Issue
Block a user