@friendlyrobot/discord-pi-agent 0.22.4 → 0.22.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.
@@ -74,12 +74,47 @@ function splitOversizedCodeBlock(token, maxChunkSize) {
74
74
  return [token.raw];
75
75
  }
76
76
  const body = token.text;
77
+ const lines = body.split("\n");
77
78
  const subChunks = [];
78
- let offset = 0;
79
- while (offset < body.length) {
80
- const segment = body.slice(offset, offset + maxBodySize);
81
- subChunks.push([header, segment, footer].join("\n"));
82
- offset += maxBodySize;
83
- }
79
+ let currentLines = [];
80
+ let currentSize = 0;
81
+ const flushChunk = () => {
82
+ if (currentLines.length === 0) {
83
+ return;
84
+ }
85
+ subChunks.push([header, currentLines.join("\n"), footer].join("\n"));
86
+ currentLines = [];
87
+ currentSize = 0;
88
+ };
89
+ const splitOversizedLine = (line) => {
90
+ const segments = [];
91
+ let offset = 0;
92
+ while (offset < line.length) {
93
+ segments.push(line.slice(offset, offset + maxBodySize));
94
+ offset += maxBodySize;
95
+ }
96
+ return segments;
97
+ };
98
+ lines.forEach((line, index) => {
99
+ const lineSize = line.length;
100
+ const newlineOverhead = currentLines.length > 0 ? 1 : 0;
101
+ if (lineSize > maxBodySize) {
102
+ flushChunk();
103
+ splitOversizedLine(line).forEach((segment) => {
104
+ subChunks.push([header, segment, footer].join("\n"));
105
+ });
106
+ if (index < lines.length - 1) {
107
+ currentLines = [""];
108
+ currentSize = 0;
109
+ }
110
+ return;
111
+ }
112
+ if (currentSize + newlineOverhead + lineSize > maxBodySize) {
113
+ flushChunk();
114
+ }
115
+ currentLines.push(line);
116
+ currentSize += (currentLines.length > 1 ? 1 : 0) + lineSize;
117
+ });
118
+ flushChunk();
84
119
  return subChunks;
85
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@friendlyrobot/discord-pi-agent",
3
- "version": "0.22.4",
3
+ "version": "0.22.5",
4
4
  "description": "Reusable Discord gateway for persistent pi agent sessions",
5
5
  "license": "MIT",
6
6
  "type": "module",