fix: newline chunking across channels
This commit is contained in:
@@ -168,6 +168,84 @@ describe("deliverOutboundPayloads", () => {
|
||||
expect(results.map((r) => r.messageId)).toEqual(["w1", "w2"]);
|
||||
});
|
||||
|
||||
it("respects newline chunk mode for WhatsApp", async () => {
|
||||
const sendWhatsApp = vi.fn().mockResolvedValue({ messageId: "w1", toJid: "jid" });
|
||||
const cfg: ClawdbotConfig = {
|
||||
channels: { whatsapp: { textChunkLimit: 4000, chunkMode: "newline" } },
|
||||
};
|
||||
|
||||
await deliverOutboundPayloads({
|
||||
cfg,
|
||||
channel: "whatsapp",
|
||||
to: "+1555",
|
||||
payloads: [{ text: "Line one\n\nLine two" }],
|
||||
deps: { sendWhatsApp },
|
||||
});
|
||||
|
||||
expect(sendWhatsApp).toHaveBeenCalledTimes(2);
|
||||
expect(sendWhatsApp).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"+1555",
|
||||
"Line one",
|
||||
expect.objectContaining({ verbose: false }),
|
||||
);
|
||||
expect(sendWhatsApp).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"+1555",
|
||||
"\nLine two",
|
||||
expect.objectContaining({ verbose: false }),
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves fenced blocks for markdown chunkers in newline mode", async () => {
|
||||
const chunker = vi.fn((text: string) => (text ? [text] : []));
|
||||
const sendText = vi.fn().mockImplementation(async ({ text }: { text: string }) => ({
|
||||
channel: "matrix" as const,
|
||||
messageId: text,
|
||||
roomId: "r1",
|
||||
}));
|
||||
const sendMedia = vi.fn().mockImplementation(async ({ text }: { text: string }) => ({
|
||||
channel: "matrix" as const,
|
||||
messageId: text,
|
||||
roomId: "r1",
|
||||
}));
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "matrix",
|
||||
source: "test",
|
||||
plugin: createOutboundTestPlugin({
|
||||
id: "matrix",
|
||||
outbound: {
|
||||
deliveryMode: "direct",
|
||||
chunker,
|
||||
chunkerMode: "markdown",
|
||||
textChunkLimit: 4000,
|
||||
sendText,
|
||||
sendMedia,
|
||||
},
|
||||
}),
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
channels: { matrix: { textChunkLimit: 4000, chunkMode: "newline" } },
|
||||
};
|
||||
const text = "```js\nconst a = 1;\nconst b = 2;\n```\nAfter";
|
||||
|
||||
await deliverOutboundPayloads({
|
||||
cfg,
|
||||
channel: "matrix",
|
||||
to: "!room",
|
||||
payloads: [{ text }],
|
||||
});
|
||||
|
||||
expect(chunker).toHaveBeenCalledTimes(2);
|
||||
expect(chunker).toHaveBeenNthCalledWith(1, "```js\nconst a = 1;\nconst b = 2;\n```", 4000);
|
||||
expect(chunker).toHaveBeenNthCalledWith(2, "After", 4000);
|
||||
});
|
||||
|
||||
it("uses iMessage media maxBytes from agent fallback", async () => {
|
||||
const sendIMessage = vi.fn().mockResolvedValue({ messageId: "i1" });
|
||||
setActivePluginRegistry(
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||
import {
|
||||
chunkByNewline,
|
||||
chunkMarkdownTextWithMode,
|
||||
resolveChunkMode,
|
||||
resolveTextChunkLimit,
|
||||
} from "../../auto-reply/chunk.js";
|
||||
import type { ReplyPayload } from "../../auto-reply/types.js";
|
||||
import { resolveChannelMediaMaxBytes } from "../../channels/plugins/media-limits.js";
|
||||
import { loadChannelOutboundAdapter } from "../../channels/plugins/outbound/load.js";
|
||||
@@ -62,6 +67,7 @@ type Chunker = (text: string, limit: number) => string[];
|
||||
|
||||
type ChannelHandler = {
|
||||
chunker: Chunker | null;
|
||||
chunkerMode?: "text" | "markdown";
|
||||
textChunkLimit?: number;
|
||||
sendText: (text: string) => Promise<OutboundDeliveryResult>;
|
||||
sendMedia: (caption: string, mediaUrl: string) => Promise<OutboundDeliveryResult>;
|
||||
@@ -121,8 +127,10 @@ function createPluginHandler(params: {
|
||||
const sendText = outbound.sendText;
|
||||
const sendMedia = outbound.sendMedia;
|
||||
const chunker = outbound.chunker ?? null;
|
||||
const chunkerMode = outbound.chunkerMode;
|
||||
return {
|
||||
chunker,
|
||||
chunkerMode,
|
||||
textChunkLimit: outbound.textChunkLimit,
|
||||
sendText: async (text) =>
|
||||
sendText({
|
||||
@@ -192,6 +200,7 @@ export async function deliverOutboundPayloads(params: {
|
||||
fallbackLimit: handler.textChunkLimit,
|
||||
})
|
||||
: undefined;
|
||||
const chunkMode = handler.chunker ? resolveChunkMode(cfg, channel, accountId) : "length";
|
||||
const isSignalChannel = channel === "signal";
|
||||
const signalTableMode = isSignalChannel
|
||||
? resolveMarkdownTableMode({ cfg, channel: "signal", accountId })
|
||||
@@ -212,6 +221,23 @@ export async function deliverOutboundPayloads(params: {
|
||||
results.push(await handler.sendText(text));
|
||||
return;
|
||||
}
|
||||
if (chunkMode === "newline") {
|
||||
const mode = handler.chunkerMode ?? "text";
|
||||
const lineChunks =
|
||||
mode === "markdown"
|
||||
? chunkMarkdownTextWithMode(text, textLimit, "newline")
|
||||
: chunkByNewline(text, textLimit, { splitLongLines: false });
|
||||
if (!lineChunks.length && text) lineChunks.push(text);
|
||||
for (const lineChunk of lineChunks) {
|
||||
const chunks = handler.chunker(lineChunk, textLimit);
|
||||
if (!chunks.length && lineChunk) chunks.push(lineChunk);
|
||||
for (const chunk of chunks) {
|
||||
throwIfAborted(abortSignal);
|
||||
results.push(await handler.sendText(chunk));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const chunks = handler.chunker(text, textLimit);
|
||||
for (const chunk of chunks) {
|
||||
throwIfAborted(abortSignal);
|
||||
|
||||
Reference in New Issue
Block a user