mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 06:07:28 +00:00
refactor: share slack text truncation
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { parseSlackBlocksInput } from "../../slack/blocks-input.js";
|
import { parseSlackBlocksInput } from "../../slack/blocks-input.js";
|
||||||
|
import { truncateSlackText } from "../../slack/truncate.js";
|
||||||
import type { ReplyPayload } from "../types.js";
|
import type { ReplyPayload } from "../types.js";
|
||||||
|
|
||||||
const SLACK_REPLY_BUTTON_ACTION_ID = "openclaw:reply_button";
|
const SLACK_REPLY_BUTTON_ACTION_ID = "openclaw:reply_button";
|
||||||
@@ -21,17 +22,6 @@ type SlackChoice = {
|
|||||||
value: string;
|
value: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function truncateSlackText(value: string, max: number): string {
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed.length <= max) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
if (max <= 1) {
|
|
||||||
return trimmed.slice(0, max);
|
|
||||||
}
|
|
||||||
return `${trimmed.slice(0, max - 1)}…`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseChoice(raw: string): SlackChoice | null {
|
function parseChoice(raw: string): SlackChoice | null {
|
||||||
const trimmed = raw.trim();
|
const trimmed = raw.trim();
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { SlackActionMiddlewareArgs } from "@slack/bolt";
|
import type { SlackActionMiddlewareArgs } from "@slack/bolt";
|
||||||
import type { Block, KnownBlock } from "@slack/web-api";
|
import type { Block, KnownBlock } from "@slack/web-api";
|
||||||
import { enqueueSystemEvent } from "../../../infra/system-events.js";
|
import { enqueueSystemEvent } from "../../../infra/system-events.js";
|
||||||
|
import { truncateSlackText } from "../../truncate.js";
|
||||||
import { authorizeSlackSystemEventSender } from "../auth.js";
|
import { authorizeSlackSystemEventSender } from "../auth.js";
|
||||||
import type { SlackMonitorContext } from "../context.js";
|
import type { SlackMonitorContext } from "../context.js";
|
||||||
import { escapeSlackMrkdwn } from "../mrkdwn.js";
|
import { escapeSlackMrkdwn } from "../mrkdwn.js";
|
||||||
@@ -53,17 +54,6 @@ type InteractionSummary = InteractionSelectionFields & {
|
|||||||
threadTs?: string;
|
threadTs?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function truncateInteractionString(
|
|
||||||
value: string,
|
|
||||||
max = SLACK_INTERACTION_STRING_MAX_CHARS,
|
|
||||||
): string {
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed.length <= max) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
return `${trimmed.slice(0, max - 1)}…`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function sanitizeSlackInteractionPayloadValue(value: unknown, key?: string): unknown {
|
function sanitizeSlackInteractionPayloadValue(value: unknown, key?: string): unknown {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -75,7 +65,7 @@ function sanitizeSlackInteractionPayloadValue(value: unknown, key?: string): unk
|
|||||||
return REDACTED_INTERACTION_VALUE;
|
return REDACTED_INTERACTION_VALUE;
|
||||||
}
|
}
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
return truncateInteractionString(value);
|
return truncateSlackText(value, SLACK_INTERACTION_STRING_MAX_CHARS);
|
||||||
}
|
}
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const sanitized = value
|
const sanitized = value
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { resolveNativeCommandsEnabled, resolveNativeSkillsEnabled } from "../../
|
|||||||
import { danger, logVerbose } from "../../globals.js";
|
import { danger, logVerbose } from "../../globals.js";
|
||||||
import { chunkItems } from "../../utils/chunk-items.js";
|
import { chunkItems } from "../../utils/chunk-items.js";
|
||||||
import type { ResolvedSlackAccount } from "../accounts.js";
|
import type { ResolvedSlackAccount } from "../accounts.js";
|
||||||
|
import { truncateSlackText } from "../truncate.js";
|
||||||
import { resolveSlackAllowListMatch, resolveSlackUserAllowed } from "./allow-list.js";
|
import { resolveSlackAllowListMatch, resolveSlackUserAllowed } from "./allow-list.js";
|
||||||
import { resolveSlackEffectiveAllowFrom } from "./auth.js";
|
import { resolveSlackEffectiveAllowFrom } from "./auth.js";
|
||||||
import { resolveSlackChannelConfig, type SlackChannelConfigResolved } from "./channel-config.js";
|
import { resolveSlackChannelConfig, type SlackChannelConfigResolved } from "./channel-config.js";
|
||||||
@@ -62,17 +63,6 @@ function loadSlashSkillCommandsRuntime() {
|
|||||||
type EncodedMenuChoice = SlackExternalArgMenuChoice;
|
type EncodedMenuChoice = SlackExternalArgMenuChoice;
|
||||||
const slackExternalArgMenuStore = createSlackExternalArgMenuStore();
|
const slackExternalArgMenuStore = createSlackExternalArgMenuStore();
|
||||||
|
|
||||||
function truncatePlainText(value: string, max: number): string {
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed.length <= max) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
if (max <= 1) {
|
|
||||||
return trimmed.slice(0, max);
|
|
||||||
}
|
|
||||||
return `${trimmed.slice(0, max - 1)}…`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildSlackArgMenuConfirm(params: { command: string; arg: string }) {
|
function buildSlackArgMenuConfirm(params: { command: string; arg: string }) {
|
||||||
const command = escapeSlackMrkdwn(params.command);
|
const command = escapeSlackMrkdwn(params.command);
|
||||||
const arg = escapeSlackMrkdwn(params.arg);
|
const arg = escapeSlackMrkdwn(params.arg);
|
||||||
@@ -255,12 +245,12 @@ function buildSlackCommandArgMenuBlocks(params: {
|
|||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
const headerText = truncatePlainText(
|
const headerText = truncateSlackText(
|
||||||
`/${params.command}: choose ${params.arg}`,
|
`/${params.command}: choose ${params.arg}`,
|
||||||
SLACK_HEADER_TEXT_MAX,
|
SLACK_HEADER_TEXT_MAX,
|
||||||
);
|
);
|
||||||
const sectionText = truncatePlainText(params.title, 3000);
|
const sectionText = truncateSlackText(params.title, 3000);
|
||||||
const contextText = truncatePlainText(
|
const contextText = truncateSlackText(
|
||||||
`Select one option to continue /${params.command} (${params.arg})`,
|
`Select one option to continue /${params.command} (${params.arg})`,
|
||||||
3000,
|
3000,
|
||||||
);
|
);
|
||||||
|
|||||||
10
src/slack/truncate.ts
Normal file
10
src/slack/truncate.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export function truncateSlackText(value: string, max: number): string {
|
||||||
|
const trimmed = value.trim();
|
||||||
|
if (trimmed.length <= max) {
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
if (max <= 1) {
|
||||||
|
return trimmed.slice(0, max);
|
||||||
|
}
|
||||||
|
return `${trimmed.slice(0, max - 1)}…`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user