@friendlyrobot/discord-pi-agent 0.4.3 → 0.4.5

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.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { DiscordPiBridge, DiscordPiBridgeConfig } from "./types";
2
2
  export { buildTimeContextPrompt, type TimeContextPromptOptions, } from "./prompt-context";
3
- export { transformMarkdownTablesToCodeBlocks } from "./markdown-table-transformer";
4
3
  export { loadDiscordPiBridgeConfigFromEnv, resolveConfig } from "./config";
5
4
  export type { AgentStatus, DiscordPiBridge, DiscordPiBridgeConfig, PromptTransform, ResolvedDiscordPiBridgeConfig, } from "./types";
6
5
  export declare function startDiscordPiBridge(config: DiscordPiBridgeConfig): Promise<DiscordPiBridge>;
package/dist/index.js CHANGED
@@ -101,6 +101,12 @@ async function collectReply(session, prompt, options = {}) {
101
101
  }
102
102
  if (finalText) {
103
103
  const transformed = await transformMarkdownTablesToCodeBlocks(finalText);
104
+ console.log("[DEBUG] Table transformation comparison:", `
105
+ === RAW finalText ===
106
+ ` + finalText + `
107
+ === TRANSFORMED ===
108
+ ` + transformed + `
109
+ === END ===`);
104
110
  return transformed;
105
111
  }
106
112
  return "No response generated.";
@@ -520,57 +526,34 @@ async function handleCommand(input, agentService, promptQueue) {
520
526
  }
521
527
 
522
528
  // src/message-chunker.ts
523
- import { Lexer as Lexer2 } from "marked";
529
+ import { marked } from "marked";
524
530
  var DISCORD_MESSAGE_LIMIT = 2000;
525
531
  var SAFE_MESSAGE_LIMIT = 1900;
526
- function chunkMessage(text) {
527
- if (text.length <= SAFE_MESSAGE_LIMIT) {
532
+ function chunkMessage(text, maxChunkSize = SAFE_MESSAGE_LIMIT) {
533
+ if (text.length <= maxChunkSize) {
528
534
  return [text];
529
535
  }
530
- const codeBlockRanges = getCodeBlockRanges(text);
536
+ const tokens = marked.lexer(text);
531
537
  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 });
538
+ let currentTokens = [];
539
+ let currentSize = 0;
540
+ const flushChunk = () => {
541
+ if (currentTokens.length > 0) {
542
+ chunks.push(currentTokens.map((t) => t.raw).join("").trim());
543
+ currentTokens = [];
544
+ currentSize = 0;
559
545
  }
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;
546
+ };
547
+ for (const token of tokens) {
548
+ const size = token.raw.length;
549
+ if (currentSize + size > maxChunkSize && currentTokens.length > 0) {
550
+ flushChunk();
571
551
  }
552
+ currentTokens.push(token);
553
+ currentSize += size;
572
554
  }
573
- return absBoundary;
555
+ flushChunk();
556
+ return chunks.map((chunk) => chunk.slice(0, DISCORD_MESSAGE_LIMIT));
574
557
  }
575
558
 
576
559
  // src/discord-client.ts
@@ -819,7 +802,6 @@ function registerSignalHandlers(stop) {
819
802
  });
820
803
  }
821
804
  export {
822
- transformMarkdownTablesToCodeBlocks,
823
805
  startDiscordPiBridge,
824
806
  resolveConfig,
825
807
  loadDiscordPiBridgeConfigFromEnv,
@@ -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.5",
4
4
  "description": "Reusable Discord gateway bridge for persistent pi agent sessions",
5
5
  "license": "MIT",
6
6
  "type": "module",