@friendlyrobot/discord-pi-agent 0.4.2 → 0.4.3

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,13 +520,16 @@ async function handleCommand(input, agentService, promptQueue) {
520
520
  }
521
521
 
522
522
  // src/message-chunker.ts
523
+ import { Lexer as Lexer2 } from "marked";
523
524
  var DISCORD_MESSAGE_LIMIT = 2000;
524
525
  var SAFE_MESSAGE_LIMIT = 1900;
525
526
  function chunkMessage(text) {
526
527
  if (text.length <= SAFE_MESSAGE_LIMIT) {
527
528
  return [text];
528
529
  }
530
+ const codeBlockRanges = getCodeBlockRanges(text);
529
531
  const chunks = [];
532
+ let offset = 0;
530
533
  let remaining = text;
531
534
  while (remaining.length > SAFE_MESSAGE_LIMIT) {
532
535
  const candidate = remaining.slice(0, SAFE_MESSAGE_LIMIT);
@@ -534,15 +537,41 @@ function chunkMessage(text) {
534
537
 
535
538
  `), candidate.lastIndexOf(`
536
539
  `), candidate.lastIndexOf(" "));
537
- const boundary = splitIndex > 0 ? splitIndex : SAFE_MESSAGE_LIMIT;
540
+ const relBoundary = splitIndex > 0 ? splitIndex : SAFE_MESSAGE_LIMIT;
541
+ const absBoundary = adjustBoundaryForCodeBlock(offset + relBoundary, codeBlockRanges);
542
+ const boundary = absBoundary - offset;
538
543
  chunks.push(remaining.slice(0, boundary).trim());
539
544
  remaining = remaining.slice(boundary).trim();
545
+ offset = absBoundary;
540
546
  }
541
547
  if (remaining.length > 0) {
542
548
  chunks.push(remaining);
543
549
  }
544
550
  return chunks.filter((chunk) => chunk.length > 0).map((chunk) => chunk.slice(0, DISCORD_MESSAGE_LIMIT));
545
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 });
559
+ }
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;
571
+ }
572
+ }
573
+ return absBoundary;
574
+ }
546
575
 
547
576
  // src/discord-client.ts
548
577
  async function startDiscordClient(config, agentService, promptQueue) {
@@ -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.3",
4
4
  "description": "Reusable Discord gateway bridge for persistent pi agent sessions",
5
5
  "license": "MIT",
6
6
  "type": "module",