refactor(zalo): share outbound chunker

This commit is contained in:
Peter Steinberger
2026-02-15 00:42:42 +00:00
parent 0d0ebd0e20
commit 56bc9b5058
6 changed files with 81 additions and 90 deletions

View File

@@ -0,0 +1,10 @@
export function formatAllowFromLowercase(params: {
allowFrom: Array<string | number>;
stripPrefixRe?: RegExp;
}): string[] {
return params.allowFrom
.map((entry) => String(entry).trim())
.filter(Boolean)
.map((entry) => (params.stripPrefixRe ? entry.replace(params.stripPrefixRe, "") : entry))
.map((entry) => entry.toLowerCase());
}

View File

@@ -0,0 +1,15 @@
import type { OpenClawConfig } from "../config/config.js";
export function resolveChannelAccountConfigBasePath(params: {
cfg: OpenClawConfig;
channelKey: string;
accountId: string;
}): string {
const channels = params.cfg.channels as unknown as Record<string, unknown> | undefined;
const channelSection = channels?.[params.channelKey] as Record<string, unknown> | undefined;
const accounts = channelSection?.accounts as Record<string, unknown> | undefined;
const useAccountPath = Boolean(accounts?.[params.accountId]);
return useAccountPath
? `channels.${params.channelKey}.accounts.${params.accountId}.`
: `channels.${params.channelKey}.`;
}

View File

@@ -126,6 +126,9 @@ export { ToolPolicySchema } from "../config/zod-schema.agent-runtime.js";
export type { RuntimeEnv } from "../runtime.js";
export type { WizardPrompter } from "../wizard/prompts.js";
export { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js";
export { formatAllowFromLowercase } from "./allow-from.js";
export { resolveChannelAccountConfigBasePath } from "./config-paths.js";
export { chunkTextForOutbound } from "./text-chunking.js";
export type { ChatType } from "../channels/chat-type.js";
/** @deprecated Use ChatType instead */
export type { RoutePeerKind } from "../routing/resolve-route.js";

View File

@@ -0,0 +1,31 @@
export function chunkTextForOutbound(text: string, limit: number): string[] {
if (!text) {
return [];
}
if (limit <= 0 || text.length <= limit) {
return [text];
}
const chunks: string[] = [];
let remaining = text;
while (remaining.length > limit) {
const window = remaining.slice(0, limit);
const lastNewline = window.lastIndexOf("\n");
const lastSpace = window.lastIndexOf(" ");
let breakIdx = lastNewline > 0 ? lastNewline : lastSpace;
if (breakIdx <= 0) {
breakIdx = limit;
}
const rawChunk = remaining.slice(0, breakIdx);
const chunk = rawChunk.trimEnd();
if (chunk.length > 0) {
chunks.push(chunk);
}
const brokeOnSeparator = breakIdx < remaining.length && /\s/.test(remaining[breakIdx]);
const nextStart = Math.min(remaining.length, breakIdx + (brokeOnSeparator ? 1 : 0));
remaining = remaining.slice(nextStart).trimStart();
}
if (remaining.length) {
chunks.push(remaining);
}
return chunks;
}