Skip to content

Commit 588a8af

Browse files
committed
Preserve long Slack answers and safely escape progress
1 parent 79d25bf commit 588a8af

3 files changed

Lines changed: 104 additions & 4 deletions

File tree

‎extensions/slack/src/monitor/message-handler/dispatch-progress.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export function createSlackProgressRuntime(runtimeParams: {
323323
mode: slackStreaming.mode,
324324
active: progressDraftActive && streamMode === "status_final",
325325
seed: progressSeed,
326-
formatLine: previewToolProgressEnabled ? formatSlackProgressDraftLine : undefined,
326+
formatLine: formatSlackProgressDraftLine,
327327
reasoningLinePrefix: "🧠 ",
328328
commentaryLinePrefix: "",
329329
reasoningGate: previewToolProgressEnabled,
@@ -439,6 +439,7 @@ export function createSlackProgressRuntime(runtimeParams: {
439439
entry: account.config,
440440
lines: legacyPreviewToolProgressLines,
441441
seed: progressSeed,
442+
formatLine: formatSlackProgressDraftLine,
442443
narration: explanation,
443444
plan: steps,
444445
});
@@ -674,5 +675,29 @@ export function createSlackProgressRuntime(runtimeParams: {
674675
}
675676

676677
function formatSlackProgressDraftLine(line: string): string {
677-
return /^(?:🧠|💬)\s/u.test(line) ? line : escapeSlackMrkdwn(line);
678+
if (/^(?:🧠|💬)\s/u.test(line)) {
679+
return line;
680+
}
681+
682+
const italicCommentary = /^_(.*)_$/su.exec(line);
683+
if (!italicCommentary) {
684+
return escapeSlackMrkdwn(line);
685+
}
686+
687+
const content = italicCommentary[1]!
688+
.split(/(`[^`\n]+`)/u)
689+
.map((segment, index) => {
690+
if (index % 2 === 0) {
691+
return escapeSlackMrkdwn(segment);
692+
}
693+
const code = segment
694+
.slice(1, -1)
695+
.replaceAll("&", "&")
696+
.replaceAll("<", "&lt;")
697+
.replaceAll(">", "&gt;");
698+
return `\`${code}\``;
699+
})
700+
.join("");
701+
702+
return `_${content}_`;
678703
}

‎extensions/slack/src/monitor/message-handler/dispatch.preview-fallback.test.ts‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ vi.mock("../../format.js", () => ({
876876

877877
vi.mock("../../limits.js", () => ({
878878
SLACK_TEXT_LIMIT: 4000,
879+
SLACK_EDIT_TEXT_MAX_BYTES: 4000,
879880
}));
880881

881882
vi.mock("../../sent-thread-cache.js", () => ({
@@ -2264,6 +2265,44 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
22642265
expect(draftStream.clear).not.toHaveBeenCalled();
22652266
});
22662267

2268+
it.each([
2269+
{ description: "plain text", finalText: "x".repeat(4001) },
2270+
{ description: "multibyte text", finalText: "é".repeat(2001) },
2271+
])(
2272+
"delivers oversized $description intact through the normal chunked sender",
2273+
async ({ finalText }) => {
2274+
const draftStream = createDraftStreamStub();
2275+
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
2276+
mockedSlackStreamingMode = "progress";
2277+
mockedSlackDraftMode = "status_final";
2278+
mockedDispatchSequence = [{ kind: "final", payload: { text: finalText } }];
2279+
mockedReplyOptionEvents = [
2280+
{
2281+
kind: "item",
2282+
itemKind: "preamble",
2283+
itemId: "preamble-1",
2284+
progressText: "Checking the full answer before replying.",
2285+
},
2286+
];
2287+
2288+
await dispatchPreparedSlackMessage(
2289+
createPreparedSlackMessage({
2290+
accountConfig: {
2291+
streaming: {
2292+
mode: "progress",
2293+
progress: { label: false, commentary: true, toolProgress: false },
2294+
},
2295+
},
2296+
}),
2297+
);
2298+
2299+
expect(finalizeSlackPreviewEditMock).not.toHaveBeenCalled();
2300+
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
2301+
expectDeliverReplyCall(0, finalText);
2302+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
2303+
},
2304+
);
2305+
22672306
it("retains the progress draft when both the final edit and fallback send fail", async () => {
22682307
const draftStream = createDraftStreamStub();
22692308
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
@@ -3744,7 +3783,39 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
37443783
);
37453784

37463785
expect(draftStream.update).toHaveBeenLastCalledWith(
3747-
"_I’m using the `monorepo` skill on Linux x86_64._",
3786+
"_I’m using the `monorepo` skill on Linux x86\\_64._",
3787+
);
3788+
});
3789+
3790+
it("escapes Slack mentions and formatting in commentary without losing outer italics or inline code", async () => {
3791+
const draftStream = createDraftStreamStub();
3792+
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
3793+
mockedSlackStreamingMode = "progress";
3794+
mockedSlackDraftMode = "status_final";
3795+
mockedDispatchSequence = [];
3796+
mockedReplyOptionEvents = [
3797+
{
3798+
kind: "item",
3799+
itemKind: "preamble",
3800+
itemId: "preamble-1",
3801+
progressText:
3802+
"checking <@U123> in <#C123> and <!channel> with *urgent* _context_ `src/one.ts`",
3803+
},
3804+
];
3805+
3806+
await dispatchPreparedSlackMessage(
3807+
createPreparedSlackMessage({
3808+
accountConfig: {
3809+
streaming: {
3810+
mode: "progress",
3811+
progress: { label: false, commentary: true, toolProgress: false },
3812+
},
3813+
},
3814+
}),
3815+
);
3816+
3817+
expect(draftStream.update).toHaveBeenLastCalledWith(
3818+
"_checking &lt;@U123&gt; in &lt;#C123&gt; and &lt;!channel&gt; with \\*urgent\\* \\_context\\_ `src/one.ts`_",
37483819
);
37493820
});
37503821

‎extensions/slack/src/monitor/message-handler/dispatch.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { ReplyDispatchKind } from "openclaw/plugin-sdk/reply-runtime";
2424
import { danger, logVerbose, shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env";
2525
import { formatSlackError } from "../../errors.js";
2626
import { normalizeSlackOutboundText } from "../../format.js";
27+
import { SLACK_EDIT_TEXT_MAX_BYTES } from "../../limits.js";
2728
import { emitSlackMessageSentHooks } from "../../message-sent-hook.js";
2829
import { resolveSlackReplyRenderPlan } from "../../reply-blocks.js";
2930
import { recordSlackThreadParticipation } from "../../sent-thread-cache.js";
@@ -32,6 +33,7 @@ import {
3233
stopSlackStream,
3334
type SlackStreamSession,
3435
} from "../../streaming.js";
36+
import { countSlackTextUtf8Bytes } from "../../truncate.js";
3537
import { resolveSlackBotLoopProtection } from "./dispatch-helpers.js";
3638
import { createSlackProgressRuntime } from "./dispatch-progress.js";
3739
import { createSlackDispatchSetup } from "./dispatch-setup.js";
@@ -163,7 +165,8 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
163165
previewStreamingEnabled &&
164166
!payload.isError &&
165167
!requiresSeparateFallbackDelivery &&
166-
trimmedFinalText.length > 0
168+
trimmedFinalText.length > 0 &&
169+
countSlackTextUtf8Bytes(previewFinalText) <= SLACK_EDIT_TEXT_MAX_BYTES
167170
) {
168171
const channelId = draftStream.channelId();
169172
const messageId = draftStream.messageId();
@@ -249,6 +252,7 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
249252
(reply.hasMedia && !ttsSupplement) ||
250253
payload.isError ||
251254
requiresSeparateFallbackDelivery ||
255+
countSlackTextUtf8Bytes(previewFinalText) > SLACK_EDIT_TEXT_MAX_BYTES ||
252256
(trimmedFinalText.length === 0 && !slackBlocks?.length)
253257
) {
254258
return undefined;

0 commit comments

Comments
 (0)