@friendlyrobot/discord-pi-agent 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +22 -16
- package/dist/message-chunker.d.ts +7 -1
- package/dist/message-chunker.test.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -520,28 +520,34 @@ async function handleCommand(input, agentService, promptQueue) {
|
|
|
520
520
|
}
|
|
521
521
|
|
|
522
522
|
// src/message-chunker.ts
|
|
523
|
+
import { marked } from "marked";
|
|
523
524
|
var DISCORD_MESSAGE_LIMIT = 2000;
|
|
524
525
|
var SAFE_MESSAGE_LIMIT = 1900;
|
|
525
|
-
function chunkMessage(text) {
|
|
526
|
-
if (text.length <=
|
|
526
|
+
function chunkMessage(text, maxChunkSize = SAFE_MESSAGE_LIMIT) {
|
|
527
|
+
if (text.length <= maxChunkSize) {
|
|
527
528
|
return [text];
|
|
528
529
|
}
|
|
530
|
+
const tokens = marked.lexer(text);
|
|
529
531
|
const chunks = [];
|
|
530
|
-
let
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
532
|
+
let currentTokens = [];
|
|
533
|
+
let currentSize = 0;
|
|
534
|
+
const flushChunk = () => {
|
|
535
|
+
if (currentTokens.length > 0) {
|
|
536
|
+
chunks.push(currentTokens.map((t) => t.raw).join("").trim());
|
|
537
|
+
currentTokens = [];
|
|
538
|
+
currentSize = 0;
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
for (const token of tokens) {
|
|
542
|
+
const size = token.raw.length;
|
|
543
|
+
if (currentSize + size > maxChunkSize && currentTokens.length > 0) {
|
|
544
|
+
flushChunk();
|
|
545
|
+
}
|
|
546
|
+
currentTokens.push(token);
|
|
547
|
+
currentSize += size;
|
|
543
548
|
}
|
|
544
|
-
|
|
549
|
+
flushChunk();
|
|
550
|
+
return chunks.map((chunk) => chunk.slice(0, DISCORD_MESSAGE_LIMIT));
|
|
545
551
|
}
|
|
546
552
|
|
|
547
553
|
// src/discord-client.ts
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Chunk markdown text safely, preserving structural integrity of
|
|
3
|
+
* code blocks, tables, lists, and other block-level elements.
|
|
4
|
+
* Uses marked's lexer to split on token boundaries so no element
|
|
5
|
+
* gets bisected mid-structure.
|
|
6
|
+
*/
|
|
7
|
+
export declare function chunkMessage(text: string, maxChunkSize?: number): string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|