feat: configurable outbound text chunk limits

This commit is contained in:
Peter Steinberger
2026-01-03 01:07:23 +01:00
parent 2d28fa34f5
commit 5684e2d658
12 changed files with 154 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { chunkText } from "./chunk.js";
import { chunkText, resolveTextChunkLimit } from "./chunk.js";
describe("chunkText", () => {
it("keeps multi-line text in one chunk when under limit", () => {
@@ -45,3 +45,30 @@ describe("chunkText", () => {
expect(chunks).toEqual(["Supercalif", "ragilistic", "expialidoc", "ious"]);
});
});
describe("resolveTextChunkLimit", () => {
it("uses per-surface defaults", () => {
expect(resolveTextChunkLimit(undefined, "whatsapp")).toBe(4000);
expect(resolveTextChunkLimit(undefined, "telegram")).toBe(4000);
expect(resolveTextChunkLimit(undefined, "signal")).toBe(4000);
expect(resolveTextChunkLimit(undefined, "imessage")).toBe(4000);
expect(resolveTextChunkLimit(undefined, "discord")).toBe(2000);
});
it("supports a global override", () => {
const cfg = { messages: { textChunkLimit: 1234 } };
expect(resolveTextChunkLimit(cfg, "whatsapp")).toBe(1234);
expect(resolveTextChunkLimit(cfg, "discord")).toBe(1234);
});
it("prefers per-surface overrides over global", () => {
const cfg = {
messages: {
textChunkLimit: 1234,
textChunkLimitBySurface: { discord: 111 },
},
};
expect(resolveTextChunkLimit(cfg, "discord")).toBe(111);
expect(resolveTextChunkLimit(cfg, "telegram")).toBe(1234);
});
});

View File

@@ -2,6 +2,43 @@
// unintentionally breaking on newlines. Using [\s\S] keeps newlines inside
// the chunk so messages are only split when they truly exceed the limit.
import type { ClawdisConfig } from "../config/config.js";
export type TextChunkSurface =
| "whatsapp"
| "telegram"
| "discord"
| "signal"
| "imessage"
| "webchat";
const DEFAULT_CHUNK_LIMIT_BY_SURFACE: Record<TextChunkSurface, number> = {
whatsapp: 4000,
telegram: 4000,
discord: 2000,
signal: 4000,
imessage: 4000,
webchat: 4000,
};
export function resolveTextChunkLimit(
cfg: Pick<ClawdisConfig, "messages"> | undefined,
surface?: TextChunkSurface,
): number {
const surfaceOverride = surface
? cfg?.messages?.textChunkLimitBySurface?.[surface]
: undefined;
if (typeof surfaceOverride === "number" && surfaceOverride > 0) {
return surfaceOverride;
}
const globalOverride = cfg?.messages?.textChunkLimit;
if (typeof globalOverride === "number" && globalOverride > 0) {
return globalOverride;
}
if (surface) return DEFAULT_CHUNK_LIMIT_BY_SURFACE[surface];
return 4000;
}
export function chunkText(text: string, limit: number): string[] {
if (!text) return [];
if (limit <= 0) return [text];