@friendlyrobot/discord-pi-agent 0.4.3 → 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,57 +520,34 @@ async function handleCommand(input, agentService, promptQueue) {
520
520
  }
521
521
 
522
522
  // src/message-chunker.ts
523
- import { Lexer as Lexer2 } from "marked";
523
+ import { marked } from "marked";
524
524
  var DISCORD_MESSAGE_LIMIT = 2000;
525
525
  var SAFE_MESSAGE_LIMIT = 1900;
526
- function chunkMessage(text) {
527
- if (text.length <= SAFE_MESSAGE_LIMIT) {
526
+ function chunkMessage(text, maxChunkSize = SAFE_MESSAGE_LIMIT) {
527
+ if (text.length <= maxChunkSize) {
528
528
  return [text];
529
529
  }
530
- const codeBlockRanges = getCodeBlockRanges(text);
530
+ const tokens = marked.lexer(text);
531
531
  const chunks = [];
532
- let offset = 0;
533
- let remaining = text;
534
- while (remaining.length > SAFE_MESSAGE_LIMIT) {
535
- const candidate = remaining.slice(0, SAFE_MESSAGE_LIMIT);
536
- const splitIndex = Math.max(candidate.lastIndexOf(`
537
-
538
- `), candidate.lastIndexOf(`
539
- `), candidate.lastIndexOf(" "));
540
- const relBoundary = splitIndex > 0 ? splitIndex : SAFE_MESSAGE_LIMIT;
541
- const absBoundary = adjustBoundaryForCodeBlock(offset + relBoundary, codeBlockRanges);
542
- const boundary = absBoundary - offset;
543
- chunks.push(remaining.slice(0, boundary).trim());
544
- remaining = remaining.slice(boundary).trim();
545
- offset = absBoundary;
546
- }
547
- if (remaining.length > 0) {
548
- chunks.push(remaining);
549
- }
550
- return chunks.filter((chunk) => chunk.length > 0).map((chunk) => chunk.slice(0, DISCORD_MESSAGE_LIMIT));
551
- }
552
- function getCodeBlockRanges(text) {
553
- const tokens = Lexer2.lex(text);
554
- const ranges = [];
555
- let pos = 0;
556
- for (const token of tokens) {
557
- if (token.type === "code") {
558
- ranges.push({ start: pos, end: pos + token.raw.length });
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;
559
539
  }
560
- pos += token.raw.length;
561
- }
562
- return ranges;
563
- }
564
- function adjustBoundaryForCodeBlock(absBoundary, codeBlockRanges) {
565
- for (const range of codeBlockRanges) {
566
- if (absBoundary > range.start && absBoundary < range.end) {
567
- if (range.end <= DISCORD_MESSAGE_LIMIT) {
568
- return range.end;
569
- }
570
- return absBoundary;
540
+ };
541
+ for (const token of tokens) {
542
+ const size = token.raw.length;
543
+ if (currentSize + size > maxChunkSize && currentTokens.length > 0) {
544
+ flushChunk();
571
545
  }
546
+ currentTokens.push(token);
547
+ currentSize += size;
572
548
  }
573
- return absBoundary;
549
+ flushChunk();
550
+ return chunks.map((chunk) => chunk.slice(0, DISCORD_MESSAGE_LIMIT));
574
551
  }
575
552
 
576
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[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@friendlyrobot/discord-pi-agent",
3
- "version": "0.4.3",
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",