@openclaw/zalouser 2026.3.11 → 2026.3.12
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/channel.sendpayload.test.ts +36 -4
- package/src/channel.test.ts +46 -11
- package/src/channel.ts +21 -6
- package/src/config-schema.ts +1 -0
- package/src/group-policy.test.ts +12 -0
- package/src/group-policy.ts +7 -4
- package/src/monitor.group-gating.test.ts +109 -1
- package/src/monitor.ts +24 -14
- package/src/send.test.ts +247 -9
- package/src/send.ts +187 -2
- package/src/text-styles.test.ts +203 -0
- package/src/text-styles.ts +537 -0
- package/src/types.ts +7 -0
- package/src/zalo-js.ts +48 -2
- package/src/zca-client.ts +33 -0
package/src/send.test.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
sendSeenZalouser,
|
|
9
9
|
sendTypingZalouser,
|
|
10
10
|
} from "./send.js";
|
|
11
|
+
import { parseZalouserTextStyles } from "./text-styles.js";
|
|
11
12
|
import {
|
|
12
13
|
sendZaloDeliveredEvent,
|
|
13
14
|
sendZaloLink,
|
|
@@ -16,6 +17,7 @@ import {
|
|
|
16
17
|
sendZaloTextMessage,
|
|
17
18
|
sendZaloTypingEvent,
|
|
18
19
|
} from "./zalo-js.js";
|
|
20
|
+
import { TextStyle } from "./zca-client.js";
|
|
19
21
|
|
|
20
22
|
vi.mock("./zalo-js.js", () => ({
|
|
21
23
|
sendZaloTextMessage: vi.fn(),
|
|
@@ -43,36 +45,272 @@ describe("zalouser send helpers", () => {
|
|
|
43
45
|
mockSendSeen.mockReset();
|
|
44
46
|
});
|
|
45
47
|
|
|
46
|
-
it("
|
|
48
|
+
it("keeps plain text literal by default", async () => {
|
|
47
49
|
mockSendText.mockResolvedValueOnce({ ok: true, messageId: "mid-1" });
|
|
48
50
|
|
|
49
|
-
const result = await sendMessageZalouser("thread-1", "hello", {
|
|
51
|
+
const result = await sendMessageZalouser("thread-1", "**hello**", {
|
|
50
52
|
profile: "default",
|
|
51
53
|
isGroup: true,
|
|
52
54
|
});
|
|
53
55
|
|
|
54
|
-
expect(mockSendText).toHaveBeenCalledWith(
|
|
56
|
+
expect(mockSendText).toHaveBeenCalledWith(
|
|
57
|
+
"thread-1",
|
|
58
|
+
"**hello**",
|
|
59
|
+
expect.objectContaining({
|
|
60
|
+
profile: "default",
|
|
61
|
+
isGroup: true,
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
expect(result).toEqual({ ok: true, messageId: "mid-1" });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("formats markdown text when markdown mode is enabled", async () => {
|
|
68
|
+
mockSendText.mockResolvedValueOnce({ ok: true, messageId: "mid-1b" });
|
|
69
|
+
|
|
70
|
+
await sendMessageZalouser("thread-1", "**hello**", {
|
|
55
71
|
profile: "default",
|
|
56
72
|
isGroup: true,
|
|
73
|
+
textMode: "markdown",
|
|
57
74
|
});
|
|
58
|
-
|
|
75
|
+
|
|
76
|
+
expect(mockSendText).toHaveBeenCalledWith(
|
|
77
|
+
"thread-1",
|
|
78
|
+
"hello",
|
|
79
|
+
expect.objectContaining({
|
|
80
|
+
profile: "default",
|
|
81
|
+
isGroup: true,
|
|
82
|
+
textMode: "markdown",
|
|
83
|
+
textStyles: [{ start: 0, len: 5, st: TextStyle.Bold }],
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
59
86
|
});
|
|
60
87
|
|
|
61
|
-
it("
|
|
88
|
+
it("formats image captions in markdown mode", async () => {
|
|
62
89
|
mockSendText.mockResolvedValueOnce({ ok: true, messageId: "mid-2" });
|
|
63
90
|
|
|
64
91
|
await sendImageZalouser("thread-2", "https://example.com/a.png", {
|
|
65
92
|
profile: "p2",
|
|
66
|
-
caption: "
|
|
93
|
+
caption: "_cap_",
|
|
67
94
|
isGroup: false,
|
|
95
|
+
textMode: "markdown",
|
|
68
96
|
});
|
|
69
97
|
|
|
70
|
-
expect(mockSendText).toHaveBeenCalledWith(
|
|
98
|
+
expect(mockSendText).toHaveBeenCalledWith(
|
|
99
|
+
"thread-2",
|
|
100
|
+
"cap",
|
|
101
|
+
expect.objectContaining({
|
|
102
|
+
profile: "p2",
|
|
103
|
+
caption: undefined,
|
|
104
|
+
isGroup: false,
|
|
105
|
+
mediaUrl: "https://example.com/a.png",
|
|
106
|
+
textMode: "markdown",
|
|
107
|
+
textStyles: [{ start: 0, len: 3, st: TextStyle.Italic }],
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("does not keep the raw markdown caption as a media fallback after formatting", async () => {
|
|
113
|
+
mockSendText.mockResolvedValueOnce({ ok: true, messageId: "mid-2b" });
|
|
114
|
+
|
|
115
|
+
await sendImageZalouser("thread-2", "https://example.com/a.png", {
|
|
71
116
|
profile: "p2",
|
|
72
|
-
caption: "
|
|
117
|
+
caption: "```\n```",
|
|
118
|
+
isGroup: false,
|
|
119
|
+
textMode: "markdown",
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
expect(mockSendText).toHaveBeenCalledWith(
|
|
123
|
+
"thread-2",
|
|
124
|
+
"",
|
|
125
|
+
expect.objectContaining({
|
|
126
|
+
profile: "p2",
|
|
127
|
+
caption: undefined,
|
|
128
|
+
isGroup: false,
|
|
129
|
+
mediaUrl: "https://example.com/a.png",
|
|
130
|
+
textMode: "markdown",
|
|
131
|
+
textStyles: undefined,
|
|
132
|
+
}),
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("rechunks normalized markdown text before sending to avoid transport truncation", async () => {
|
|
137
|
+
const text = "\t".repeat(500) + "a".repeat(1500);
|
|
138
|
+
const formatted = parseZalouserTextStyles(text);
|
|
139
|
+
mockSendText
|
|
140
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2c-1" })
|
|
141
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2c-2" });
|
|
142
|
+
|
|
143
|
+
const result = await sendMessageZalouser("thread-2c", text, {
|
|
144
|
+
profile: "p2c",
|
|
145
|
+
isGroup: false,
|
|
146
|
+
textMode: "markdown",
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(formatted.text.length).toBeGreaterThan(2000);
|
|
150
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
151
|
+
expect(mockSendText.mock.calls.map((call) => call[1]).join("")).toBe(formatted.text);
|
|
152
|
+
expect(mockSendText.mock.calls.every((call) => (call[1] as string).length <= 2000)).toBe(true);
|
|
153
|
+
expect(result).toEqual({ ok: true, messageId: "mid-2c-2" });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("preserves text styles when splitting long formatted markdown", async () => {
|
|
157
|
+
const text = `**${"a".repeat(2501)}**`;
|
|
158
|
+
mockSendText
|
|
159
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2d-1" })
|
|
160
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2d-2" });
|
|
161
|
+
|
|
162
|
+
const result = await sendMessageZalouser("thread-2d", text, {
|
|
163
|
+
profile: "p2d",
|
|
164
|
+
isGroup: false,
|
|
165
|
+
textMode: "markdown",
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
169
|
+
1,
|
|
170
|
+
"thread-2d",
|
|
171
|
+
"a".repeat(2000),
|
|
172
|
+
expect.objectContaining({
|
|
173
|
+
profile: "p2d",
|
|
174
|
+
isGroup: false,
|
|
175
|
+
textMode: "markdown",
|
|
176
|
+
textStyles: [{ start: 0, len: 2000, st: TextStyle.Bold }],
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
180
|
+
2,
|
|
181
|
+
"thread-2d",
|
|
182
|
+
"a".repeat(501),
|
|
183
|
+
expect.objectContaining({
|
|
184
|
+
profile: "p2d",
|
|
185
|
+
isGroup: false,
|
|
186
|
+
textMode: "markdown",
|
|
187
|
+
textStyles: [{ start: 0, len: 501, st: TextStyle.Bold }],
|
|
188
|
+
}),
|
|
189
|
+
);
|
|
190
|
+
expect(result).toEqual({ ok: true, messageId: "mid-2d-2" });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("preserves formatted text and styles when newline chunk mode splits after parsing", async () => {
|
|
194
|
+
const text = `**${"a".repeat(1995)}**\n\nsecond paragraph`;
|
|
195
|
+
const formatted = parseZalouserTextStyles(text);
|
|
196
|
+
mockSendText
|
|
197
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2d-3" })
|
|
198
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2d-4" });
|
|
199
|
+
|
|
200
|
+
const result = await sendMessageZalouser("thread-2d-2", text, {
|
|
201
|
+
profile: "p2d-2",
|
|
202
|
+
isGroup: false,
|
|
203
|
+
textMode: "markdown",
|
|
204
|
+
textChunkMode: "newline",
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
208
|
+
expect(mockSendText.mock.calls.map((call) => call[1]).join("")).toBe(formatted.text);
|
|
209
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
210
|
+
1,
|
|
211
|
+
"thread-2d-2",
|
|
212
|
+
`${"a".repeat(1995)}\n\n`,
|
|
213
|
+
expect.objectContaining({
|
|
214
|
+
profile: "p2d-2",
|
|
215
|
+
isGroup: false,
|
|
216
|
+
textMode: "markdown",
|
|
217
|
+
textChunkMode: "newline",
|
|
218
|
+
textStyles: [{ start: 0, len: 1995, st: TextStyle.Bold }],
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
222
|
+
2,
|
|
223
|
+
"thread-2d-2",
|
|
224
|
+
"second paragraph",
|
|
225
|
+
expect.objectContaining({
|
|
226
|
+
profile: "p2d-2",
|
|
227
|
+
isGroup: false,
|
|
228
|
+
textMode: "markdown",
|
|
229
|
+
textChunkMode: "newline",
|
|
230
|
+
textStyles: undefined,
|
|
231
|
+
}),
|
|
232
|
+
);
|
|
233
|
+
expect(result).toEqual({ ok: true, messageId: "mid-2d-4" });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("respects an explicit text chunk limit when splitting formatted markdown", async () => {
|
|
237
|
+
const text = `**${"a".repeat(1501)}**`;
|
|
238
|
+
mockSendText
|
|
239
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2d-5" })
|
|
240
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2d-6" });
|
|
241
|
+
|
|
242
|
+
const result = await sendMessageZalouser("thread-2d-3", text, {
|
|
243
|
+
profile: "p2d-3",
|
|
244
|
+
isGroup: false,
|
|
245
|
+
textMode: "markdown",
|
|
246
|
+
textChunkLimit: 1200,
|
|
247
|
+
} as never);
|
|
248
|
+
|
|
249
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
250
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
251
|
+
1,
|
|
252
|
+
"thread-2d-3",
|
|
253
|
+
"a".repeat(1200),
|
|
254
|
+
expect.objectContaining({
|
|
255
|
+
profile: "p2d-3",
|
|
256
|
+
isGroup: false,
|
|
257
|
+
textMode: "markdown",
|
|
258
|
+
textChunkLimit: 1200,
|
|
259
|
+
textStyles: [{ start: 0, len: 1200, st: TextStyle.Bold }],
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
262
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
263
|
+
2,
|
|
264
|
+
"thread-2d-3",
|
|
265
|
+
"a".repeat(301),
|
|
266
|
+
expect.objectContaining({
|
|
267
|
+
profile: "p2d-3",
|
|
268
|
+
isGroup: false,
|
|
269
|
+
textMode: "markdown",
|
|
270
|
+
textChunkLimit: 1200,
|
|
271
|
+
textStyles: [{ start: 0, len: 301, st: TextStyle.Bold }],
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
expect(result).toEqual({ ok: true, messageId: "mid-2d-6" });
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("sends overflow markdown captions as follow-up text after the media message", async () => {
|
|
278
|
+
const caption = "\t".repeat(500) + "a".repeat(1500);
|
|
279
|
+
const formatted = parseZalouserTextStyles(caption);
|
|
280
|
+
mockSendText
|
|
281
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2e-1" })
|
|
282
|
+
.mockResolvedValueOnce({ ok: true, messageId: "mid-2e-2" });
|
|
283
|
+
|
|
284
|
+
const result = await sendImageZalouser("thread-2e", "https://example.com/long.png", {
|
|
285
|
+
profile: "p2e",
|
|
286
|
+
caption,
|
|
73
287
|
isGroup: false,
|
|
74
|
-
|
|
288
|
+
textMode: "markdown",
|
|
75
289
|
});
|
|
290
|
+
|
|
291
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
292
|
+
expect(mockSendText.mock.calls.map((call) => call[1]).join("")).toBe(formatted.text);
|
|
293
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
294
|
+
1,
|
|
295
|
+
"thread-2e",
|
|
296
|
+
expect.any(String),
|
|
297
|
+
expect.objectContaining({
|
|
298
|
+
profile: "p2e",
|
|
299
|
+
caption: undefined,
|
|
300
|
+
isGroup: false,
|
|
301
|
+
mediaUrl: "https://example.com/long.png",
|
|
302
|
+
textMode: "markdown",
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
expect(mockSendText).toHaveBeenNthCalledWith(
|
|
306
|
+
2,
|
|
307
|
+
"thread-2e",
|
|
308
|
+
expect.any(String),
|
|
309
|
+
expect.not.objectContaining({
|
|
310
|
+
mediaUrl: "https://example.com/long.png",
|
|
311
|
+
}),
|
|
312
|
+
);
|
|
313
|
+
expect(result).toEqual({ ok: true, messageId: "mid-2e-2" });
|
|
76
314
|
});
|
|
77
315
|
|
|
78
316
|
it("delegates link helper to JS transport", async () => {
|
package/src/send.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseZalouserTextStyles } from "./text-styles.js";
|
|
1
2
|
import type { ZaloEventMessage, ZaloSendOptions, ZaloSendResult } from "./types.js";
|
|
2
3
|
import {
|
|
3
4
|
sendZaloDeliveredEvent,
|
|
@@ -7,16 +8,58 @@ import {
|
|
|
7
8
|
sendZaloTextMessage,
|
|
8
9
|
sendZaloTypingEvent,
|
|
9
10
|
} from "./zalo-js.js";
|
|
11
|
+
import { TextStyle } from "./zca-client.js";
|
|
10
12
|
|
|
11
13
|
export type ZalouserSendOptions = ZaloSendOptions;
|
|
12
14
|
export type ZalouserSendResult = ZaloSendResult;
|
|
13
15
|
|
|
16
|
+
const ZALO_TEXT_LIMIT = 2000;
|
|
17
|
+
const DEFAULT_TEXT_CHUNK_MODE = "length";
|
|
18
|
+
|
|
19
|
+
type StyledTextChunk = {
|
|
20
|
+
text: string;
|
|
21
|
+
styles?: ZaloSendOptions["textStyles"];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type TextChunkMode = NonNullable<ZaloSendOptions["textChunkMode"]>;
|
|
25
|
+
|
|
14
26
|
export async function sendMessageZalouser(
|
|
15
27
|
threadId: string,
|
|
16
28
|
text: string,
|
|
17
29
|
options: ZalouserSendOptions = {},
|
|
18
30
|
): Promise<ZalouserSendResult> {
|
|
19
|
-
|
|
31
|
+
const prepared =
|
|
32
|
+
options.textMode === "markdown"
|
|
33
|
+
? parseZalouserTextStyles(text)
|
|
34
|
+
: { text, styles: options.textStyles };
|
|
35
|
+
const textChunkLimit = options.textChunkLimit ?? ZALO_TEXT_LIMIT;
|
|
36
|
+
const chunks = splitStyledText(
|
|
37
|
+
prepared.text,
|
|
38
|
+
(prepared.styles?.length ?? 0) > 0 ? prepared.styles : undefined,
|
|
39
|
+
textChunkLimit,
|
|
40
|
+
options.textChunkMode,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
let lastResult: ZalouserSendResult | null = null;
|
|
44
|
+
for (const [index, chunk] of chunks.entries()) {
|
|
45
|
+
const chunkOptions =
|
|
46
|
+
index === 0
|
|
47
|
+
? { ...options, textStyles: chunk.styles }
|
|
48
|
+
: {
|
|
49
|
+
...options,
|
|
50
|
+
caption: undefined,
|
|
51
|
+
mediaLocalRoots: undefined,
|
|
52
|
+
mediaUrl: undefined,
|
|
53
|
+
textStyles: chunk.styles,
|
|
54
|
+
};
|
|
55
|
+
const result = await sendZaloTextMessage(threadId, chunk.text, chunkOptions);
|
|
56
|
+
if (!result.ok) {
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
lastResult = result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return lastResult ?? { ok: false, error: "No message content provided" };
|
|
20
63
|
}
|
|
21
64
|
|
|
22
65
|
export async function sendImageZalouser(
|
|
@@ -24,8 +67,9 @@ export async function sendImageZalouser(
|
|
|
24
67
|
imageUrl: string,
|
|
25
68
|
options: ZalouserSendOptions = {},
|
|
26
69
|
): Promise<ZalouserSendResult> {
|
|
27
|
-
return await
|
|
70
|
+
return await sendMessageZalouser(threadId, options.caption ?? "", {
|
|
28
71
|
...options,
|
|
72
|
+
caption: undefined,
|
|
29
73
|
mediaUrl: imageUrl,
|
|
30
74
|
});
|
|
31
75
|
}
|
|
@@ -85,3 +129,144 @@ export async function sendSeenZalouser(params: {
|
|
|
85
129
|
}): Promise<void> {
|
|
86
130
|
await sendZaloSeenEvent(params);
|
|
87
131
|
}
|
|
132
|
+
|
|
133
|
+
function splitStyledText(
|
|
134
|
+
text: string,
|
|
135
|
+
styles: ZaloSendOptions["textStyles"],
|
|
136
|
+
limit: number,
|
|
137
|
+
mode: ZaloSendOptions["textChunkMode"],
|
|
138
|
+
): StyledTextChunk[] {
|
|
139
|
+
if (text.length === 0) {
|
|
140
|
+
return [{ text, styles: undefined }];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const chunks: StyledTextChunk[] = [];
|
|
144
|
+
for (const range of splitTextRanges(text, limit, mode ?? DEFAULT_TEXT_CHUNK_MODE)) {
|
|
145
|
+
const { start, end } = range;
|
|
146
|
+
chunks.push({
|
|
147
|
+
text: text.slice(start, end),
|
|
148
|
+
styles: sliceTextStyles(styles, start, end),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return chunks;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function sliceTextStyles(
|
|
155
|
+
styles: ZaloSendOptions["textStyles"],
|
|
156
|
+
start: number,
|
|
157
|
+
end: number,
|
|
158
|
+
): ZaloSendOptions["textStyles"] {
|
|
159
|
+
if (!styles || styles.length === 0) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const chunkStyles = styles
|
|
164
|
+
.map((style) => {
|
|
165
|
+
const overlapStart = Math.max(style.start, start);
|
|
166
|
+
const overlapEnd = Math.min(style.start + style.len, end);
|
|
167
|
+
if (overlapEnd <= overlapStart) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (style.st === TextStyle.Indent) {
|
|
172
|
+
return {
|
|
173
|
+
start: overlapStart - start,
|
|
174
|
+
len: overlapEnd - overlapStart,
|
|
175
|
+
st: style.st,
|
|
176
|
+
indentSize: style.indentSize,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
start: overlapStart - start,
|
|
182
|
+
len: overlapEnd - overlapStart,
|
|
183
|
+
st: style.st,
|
|
184
|
+
};
|
|
185
|
+
})
|
|
186
|
+
.filter((style): style is NonNullable<typeof style> => style !== null);
|
|
187
|
+
|
|
188
|
+
return chunkStyles.length > 0 ? chunkStyles : undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function splitTextRanges(
|
|
192
|
+
text: string,
|
|
193
|
+
limit: number,
|
|
194
|
+
mode: TextChunkMode,
|
|
195
|
+
): Array<{ start: number; end: number }> {
|
|
196
|
+
if (mode === "newline") {
|
|
197
|
+
return splitTextRangesByPreferredBreaks(text, limit);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const ranges: Array<{ start: number; end: number }> = [];
|
|
201
|
+
for (let start = 0; start < text.length; start += limit) {
|
|
202
|
+
ranges.push({
|
|
203
|
+
start,
|
|
204
|
+
end: Math.min(text.length, start + limit),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return ranges;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function splitTextRangesByPreferredBreaks(
|
|
211
|
+
text: string,
|
|
212
|
+
limit: number,
|
|
213
|
+
): Array<{ start: number; end: number }> {
|
|
214
|
+
const ranges: Array<{ start: number; end: number }> = [];
|
|
215
|
+
let start = 0;
|
|
216
|
+
|
|
217
|
+
while (start < text.length) {
|
|
218
|
+
const maxEnd = Math.min(text.length, start + limit);
|
|
219
|
+
let end = maxEnd;
|
|
220
|
+
if (maxEnd < text.length) {
|
|
221
|
+
end =
|
|
222
|
+
findParagraphBreak(text, start, maxEnd) ??
|
|
223
|
+
findLastBreak(text, "\n", start, maxEnd) ??
|
|
224
|
+
findLastWhitespaceBreak(text, start, maxEnd) ??
|
|
225
|
+
maxEnd;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (end <= start) {
|
|
229
|
+
end = maxEnd;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
ranges.push({ start, end });
|
|
233
|
+
start = end;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return ranges;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function findParagraphBreak(text: string, start: number, end: number): number | undefined {
|
|
240
|
+
const slice = text.slice(start, end);
|
|
241
|
+
const matches = slice.matchAll(/\n[\t ]*\n+/g);
|
|
242
|
+
let lastMatch: RegExpMatchArray | undefined;
|
|
243
|
+
for (const match of matches) {
|
|
244
|
+
lastMatch = match;
|
|
245
|
+
}
|
|
246
|
+
if (!lastMatch || lastMatch.index === undefined) {
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
return start + lastMatch.index + lastMatch[0].length;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function findLastBreak(
|
|
253
|
+
text: string,
|
|
254
|
+
marker: string,
|
|
255
|
+
start: number,
|
|
256
|
+
end: number,
|
|
257
|
+
): number | undefined {
|
|
258
|
+
const index = text.lastIndexOf(marker, end - 1);
|
|
259
|
+
if (index < start) {
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
return index + marker.length;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function findLastWhitespaceBreak(text: string, start: number, end: number): number | undefined {
|
|
266
|
+
for (let index = end - 1; index > start; index -= 1) {
|
|
267
|
+
if (/\s/.test(text[index])) {
|
|
268
|
+
return index + 1;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|