@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 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 <= SAFE_MESSAGE_LIMIT) {
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 remaining = text;
531
- while (remaining.length > SAFE_MESSAGE_LIMIT) {
532
- const candidate = remaining.slice(0, SAFE_MESSAGE_LIMIT);
533
- const splitIndex = Math.max(candidate.lastIndexOf(`
534
-
535
- `), candidate.lastIndexOf(`
536
- `), candidate.lastIndexOf(" "));
537
- const boundary = splitIndex > 0 ? splitIndex : SAFE_MESSAGE_LIMIT;
538
- chunks.push(remaining.slice(0, boundary).trim());
539
- remaining = remaining.slice(boundary).trim();
540
- }
541
- if (remaining.length > 0) {
542
- chunks.push(remaining);
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
- return chunks.filter((chunk) => chunk.length > 0).map((chunk) => chunk.slice(0, DISCORD_MESSAGE_LIMIT));
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
- export declare function chunkMessage(text: string): string[];
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 {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@friendlyrobot/discord-pi-agent",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Reusable Discord gateway bridge for persistent pi agent sessions",
5
5
  "license": "MIT",
6
6
  "type": "module",