mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(zalo): share outbound chunker
This commit is contained in:
10
src/plugin-sdk/allow-from.ts
Normal file
10
src/plugin-sdk/allow-from.ts
Normal 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());
|
||||
}
|
||||
15
src/plugin-sdk/config-paths.ts
Normal file
15
src/plugin-sdk/config-paths.ts
Normal 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}.`;
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
31
src/plugin-sdk/text-chunking.ts
Normal file
31
src/plugin-sdk/text-chunking.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user