feat: improve outbound message chunking with code-block-aware splitting

- Increase max message length from 4000 to 8000 chars
- Add splitAtBoundary: paragraph > newline > space priority, never split
  inside fenced code blocks (move split to before opening fence)
- Add sendLongText: sends multi-chunk long messages sequentially
- Update PatchableTextStream.flush to use splitAtBoundary
- Add unit tests (6) for splitting edge cases
This commit is contained in:
2026-07-08 16:57:53 +08:00
parent 4736610cf3
commit bef10149a9
4 changed files with 196 additions and 10 deletions
+16 -1
View File
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { sendFile, sendTextMessage, type FeishuRuntime } from "../../src/feishu/client.js";
import { sendFile, sendLongText, sendTextMessage, type FeishuRuntime } from "../../src/feishu/client.js";
describe("Feishu client helpers", () => {
it("accepts top-level file_key from the Lark SDK file upload response", async () => {
@@ -32,6 +32,21 @@ describe("Feishu client helpers", () => {
await expect(sendTextMessage(rt, "chat-1", "hello")).resolves.toBe("message-1");
});
it("sends long text in chunks and returns the last successful message id", async () => {
let messageNumber = 0;
const messageCreate = vi.fn(async () => ({ data: { message_id: `message-${++messageNumber}` } }));
const rt = mockRuntime({ fileCreate: vi.fn(), messageCreate });
await expect(sendLongText(rt, "chat-1", "x".repeat(16_005))).resolves.toBe("message-3");
expect(messageCreate).toHaveBeenCalledTimes(3);
const sentTexts = messageCreate.mock.calls.map(([payload]) => {
const content = (payload as { data: { content: string } }).data.content;
return (JSON.parse(content) as { text: string }).text;
});
expect(sentTexts.map((text) => text.length)).toEqual([8000, 8000, 5]);
});
});
function mockRuntime(options: {
+43
View File
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { splitAtBoundary } from "../../src/feishu/textStream.js";
describe("splitAtBoundary", () => {
it("keeps short text in a single chunk", () => {
expect(splitAtBoundary("short text", 20)).toEqual(["short text"]);
});
it("keeps text at exactly maxLength in a single chunk", () => {
const text = "x".repeat(20);
expect(splitAtBoundary(text, 20)).toEqual([text]);
});
it("splits text exceeding maxLength into chunks at or below maxLength", () => {
const chunks = splitAtBoundary("x".repeat(45), 20);
expect(chunks).toHaveLength(3);
expect(chunks.every((chunk) => chunk.length <= 20)).toBe(true);
expect(chunks.join("")).toBe("x".repeat(45));
});
it("moves a split before a fenced code block when the boundary lands inside it", () => {
const prefix = "Before code starts\n";
const codeBlock = "```ts\nx\n```\n";
const text = `${prefix}${codeBlock}After`;
const chunks = splitAtBoundary(text, prefix.length + 4);
expect(chunks).toEqual([prefix, `${codeBlock}After`]);
});
it("prefers paragraph boundaries over newline boundaries", () => {
const text = "para one\n\nline two\nline three";
expect(splitAtBoundary(text, 22)).toEqual(["para one\n\n", "line two\nline three"]);
});
it("prefers newline boundaries over space boundaries", () => {
const text = "alpha beta\ngamma delta epsilon";
expect(splitAtBoundary(text, 24)).toEqual(["alpha beta\n", "gamma delta epsilon"]);
});
});