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: {