Skip to content

Commit 3fa4805

Browse files
steipeteyxshee
andauthored
fix(slack): preserve pasted inbound tables (#115163)
* fix(slack): preserve pasted inbound tables Co-authored-by: yash <[email protected]> * fix(slack): keep inbound text selection private Co-authored-by: yash <[email protected]> * fix(slack): exclude unfurl tables from inbound context Co-authored-by: yash <[email protected]> * refactor(slack): clarify aggregate table size limit Co-authored-by: yash <[email protected]> * docs(slack): clarify table fallback escaping Co-authored-by: yash <[email protected]> --------- Co-authored-by: yash <[email protected]>
1 parent 49c62f3 commit 3fa4805

17 files changed

Lines changed: 700 additions & 91 deletions

‎docs/channels/slack.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,13 @@ text from valid raw `data_table` cells, while malformed custom blocks may
16931693
degrade to their caption or general Block Kit fallback. Portable agent, CLI,
16941694
and plugin output should use `presentation`.
16951695

1696+
Slack clients can also deliver pasted spreadsheet content as a legacy `table`
1697+
block in the message's top-level blocks or attachments. OpenClaw renders those
1698+
inbound cells as delimiter-safe TSV for live agent input, thread context, and
1699+
Slack `read` actions. Only native table blocks are admitted from ordinary
1700+
attachments; link-unfurl and other non-forwarded attachment text remains
1701+
excluded.
1702+
16961703
## Plugin-owned modal submissions
16971704

16981705
Slack plugins that register an interactive handler can also receive modal

‎extensions/slack/src/actions.read.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,73 @@ describe("Slack read actions", () => {
121121
expect(result.messages.map((message) => message.ts)).toEqual(["1"]);
122122
});
123123

124+
it("renders attachment tables in channel reads without dropping raw payloads", async () => {
125+
const client = createClient();
126+
const blocks = [
127+
{
128+
type: "table",
129+
rows: [
130+
[
131+
{ type: "raw_text", text: "ID" },
132+
{ type: "raw_text", text: "Status" },
133+
],
134+
[
135+
{ type: "raw_number", value: 12345 },
136+
{ type: "raw_text", text: "enabled" },
137+
],
138+
],
139+
},
140+
];
141+
const attachments = [{ fallback: "[no preview available]", blocks }];
142+
client.conversations.history.mockResolvedValueOnce({
143+
messages: [{ ts: "1", text: " Please check these. ", attachments }],
144+
has_more: false,
145+
});
146+
147+
const result = await readSlackMessages("C1", { client, token: "xoxb-test" });
148+
149+
expect(result.messages[0]?.text).toBe(" Please check these. \nID\tStatus\n12345\tenabled");
150+
expect(result.messages[0]?.attachments).toBe(attachments);
151+
expect(result.messages[0]?.attachments?.[0]?.blocks).toBe(blocks);
152+
});
153+
154+
it("renders attachment tables in thread reads", async () => {
155+
const client = createClient();
156+
client.conversations.replies.mockResolvedValueOnce({
157+
messages: [
158+
{ ts: "1.000", text: "parent" },
159+
{
160+
ts: "1.001",
161+
text: "Thread data",
162+
attachments: [
163+
{
164+
blocks: [
165+
{
166+
type: "table",
167+
rows: [
168+
[{ type: "raw_text", text: "Name" }],
169+
[{ type: "raw_text", text: "Example A" }],
170+
],
171+
},
172+
],
173+
},
174+
],
175+
},
176+
],
177+
has_more: false,
178+
});
179+
180+
const result = await readSlackMessages("C1", {
181+
client,
182+
threadId: "1.000",
183+
token: "xoxb-test",
184+
});
185+
186+
expect(result.messages.map((message) => message.text)).toEqual([
187+
"Thread data\nName\nExample A",
188+
]);
189+
});
190+
124191
it("filters a specific channel message by messageId", async () => {
125192
const client = createClient();
126193
client.conversations.history.mockResolvedValueOnce({

‎extensions/slack/src/actions.ts‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { validateSlackBlocksArray } from "./blocks-input.js";
1111
import { createSlackLookupClient, getSlackWriteClient } from "./client.js";
1212
import { buildSlackEditTextPayload } from "./edit-text.js";
1313
import { SLACK_EDIT_TEXT_LIMIT } from "./limits.js";
14+
import { hasSlackMessageTableBlock, resolveSlackMessageText } from "./monitor/block-text.js";
1415
import { resolveSlackMedia } from "./monitor/media.js";
1516
import type { SlackMediaResult } from "./monitor/media.js";
1617
import { escapeSlackMrkdwn } from "./monitor/mrkdwn.js";
@@ -24,6 +25,7 @@ import { buildSlackNativeDataDeliveryPlan } from "./native-data-fallback.js";
2425
import { sendMessageSlack } from "./send.js";
2526
import { resolveSlackBotToken } from "./token.js";
2627
import { truncateSlackText } from "./truncate.js";
28+
import type { SlackAttachment } from "./types.js";
2729

2830
export type SlackActionClientOpts = {
2931
cfg?: OpenClawConfig;
@@ -37,6 +39,8 @@ export type SlackMessageSummary = {
3739
text?: string;
3840
user?: string;
3941
thread_ts?: string;
42+
blocks?: unknown[];
43+
attachments?: SlackAttachment[];
4044
reply_count?: number;
4145
reactions?: Array<{
4246
name?: string;
@@ -51,6 +55,14 @@ export type SlackMessageSummary = {
5155
}>;
5256
};
5357

58+
function renderSlackReadMessageText(message: SlackMessageSummary): SlackMessageSummary {
59+
if (!hasSlackMessageTableBlock(message)) {
60+
return message;
61+
}
62+
const text = resolveSlackMessageText(message, { preserveMessageTextWhitespace: true });
63+
return text && text !== message.text ? { ...message, text } : message;
64+
}
65+
5466
export type SlackPin = {
5567
type?: string;
5668
message?: { ts?: string; text?: string };
@@ -478,13 +490,15 @@ export async function readSlackMessages(
478490
limit: readLimit,
479491
...exactBounds,
480492
});
481-
const messages = ((result.messages ?? []) as SlackMessageSummary[]).filter((message) => {
482-
if (exactMessageId) {
483-
return message.ts === exactMessageId;
484-
}
485-
// conversations.replies includes the parent message; drop it for replies-only reads.
486-
return message.ts !== opts.threadId;
487-
});
493+
const messages = ((result.messages ?? []) as SlackMessageSummary[])
494+
.filter((message) => {
495+
if (exactMessageId) {
496+
return message.ts === exactMessageId;
497+
}
498+
// conversations.replies includes the parent message; drop it for replies-only reads.
499+
return message.ts !== opts.threadId;
500+
})
501+
.map(renderSlackReadMessageText);
488502
return {
489503
messages,
490504
hasMore: exactMessageId ? false : Boolean(result.has_more),
@@ -496,9 +510,9 @@ export async function readSlackMessages(
496510
limit: readLimit,
497511
...exactBounds,
498512
});
499-
const messages = ((result.messages ?? []) as SlackMessageSummary[]).filter(
500-
(message) => !exactMessageId || message.ts === exactMessageId,
501-
);
513+
const messages = ((result.messages ?? []) as SlackMessageSummary[])
514+
.filter((message) => !exactMessageId || message.ts === exactMessageId)
515+
.map(renderSlackReadMessageText);
502516
return {
503517
messages,
504518
hasMore: exactMessageId ? false : Boolean(result.has_more),

‎extensions/slack/src/blocks-fallback.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import {
33
renderSlackDataTableFallbackText,
44
renderSlackDataTableMrkdwnFallbackText,
5+
renderSlackTableFallbackText,
6+
renderSlackTableMrkdwnFallbackText,
57
} from "./data-table.js";
68
import {
79
renderSlackDataVisualizationFallbackText,
@@ -266,6 +268,10 @@ export function renderSlackBlockFallbackText(
266268
return options.nativeDataFormat === "plain"
267269
? renderSlackDataTableFallbackText(block)
268270
: renderSlackDataTableMrkdwnFallbackText(block);
271+
case "table":
272+
return options.nativeDataFormat === "plain"
273+
? renderSlackTableFallbackText(block)
274+
: renderSlackTableMrkdwnFallbackText(block);
269275
default:
270276
return undefined;
271277
}

‎extensions/slack/src/blocks-render.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
buildSlackDataTableBlock,
2222
countSlackDataTableBlocksCellCharacters,
2323
countSlackDataTableCellCharacters,
24-
SLACK_DATA_TABLE_CELL_CHARACTERS_MAX,
24+
SLACK_DATA_TABLE_AGGREGATE_CELL_CHARACTERS_MAX,
2525
} from "./data-table.js";
2626
import {
2727
buildSlackDataVisualizationBlock,
@@ -212,7 +212,8 @@ function readSlackOpenClawBlockIndex(blockId: string, prefix: string): number |
212212
export function resolveSlackBlockOffsets(blocks?: readonly SlackBlock[]): SlackBlockRenderOptions {
213213
let buttonIndexOffset = 0;
214214
const dataTableCellCharacterCountOffset =
215-
countSlackDataTableBlocksCellCharacters(blocks) ?? SLACK_DATA_TABLE_CELL_CHARACTERS_MAX + 1;
215+
countSlackDataTableBlocksCellCharacters(blocks) ??
216+
SLACK_DATA_TABLE_AGGREGATE_CELL_CHARACTERS_MAX + 1;
216217
let dataVisualizationCountOffset = 0;
217218
let selectIndexOffset = 0;
218219
for (const block of blocks ?? []) {

‎extensions/slack/src/blocks.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ describe("buildSlackBlocksFallbackText", () => {
6060
).toBe("Pipeline report (table)\n- Account: Acme; ARR: 125000");
6161
});
6262

63+
it("renders inbound table cells as bounded delimiter-safe TSV", () => {
64+
const table = {
65+
type: "table",
66+
rows: [
67+
[
68+
{ type: "raw_text", text: "Name" },
69+
{ type: "raw_number", value: 42 },
70+
{ type: "raw_text", text: "Note" },
71+
],
72+
[
73+
{
74+
type: "rich_text",
75+
elements: [
76+
{
77+
type: "rich_text_section",
78+
elements: [
79+
{ type: "text", text: "Ada" },
80+
{ type: "text", text: " " },
81+
{ type: "user", user_id: "U123" },
82+
],
83+
},
84+
],
85+
},
86+
{ type: "raw_number", value: 7, text: "seven" },
87+
{ type: "raw_text", text: "A\tB\nC\\D" },
88+
],
89+
],
90+
};
91+
92+
expect(renderSlackBlockFallbackText(table, { nativeDataFormat: "plain" })).toBe(
93+
["Name\t42\tNote", "Ada <@U123>\tseven\tA\\tB\\nC\\\\D"].join("\n"),
94+
);
95+
expect(renderSlackBlockFallbackText(table)).toBe(
96+
["Name\t42\tNote", "Ada &lt;@U123&gt;\tseven\tA\\tB\\nC\\\\D"].join("\n"),
97+
);
98+
});
99+
100+
it("rejects inbound tables outside Slack's published row limit", () => {
101+
const rows = Array.from({ length: 101 }, () => [{ type: "raw_text", text: "cell" }]);
102+
expect(renderSlackBlockFallbackText({ type: "table", rows })).toBeUndefined();
103+
});
104+
63105
it("uses only visible action labels and select placeholders", () => {
64106
const fallback = buildSlackBlocksFallbackText([
65107
{

‎extensions/slack/src/data-table.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
hasSlackDataTableBlock,
77
renderSlackDataTableCompactPlainTextFallback,
88
renderSlackDataTableFallbackText,
9-
SLACK_DATA_TABLE_CELL_CHARACTERS_MAX,
9+
SLACK_DATA_TABLE_AGGREGATE_CELL_CHARACTERS_MAX,
1010
} from "./data-table.js";
1111

1212
describe("Slack data table blocks", () => {
@@ -69,12 +69,12 @@ describe("Slack data table blocks", () => {
6969
).toBeUndefined();
7070
expect(
7171
buildSlackDataTableBlock(base, {
72-
cellCharacterCountOffset: SLACK_DATA_TABLE_CELL_CHARACTERS_MAX - 7,
72+
cellCharacterCountOffset: SLACK_DATA_TABLE_AGGREGATE_CELL_CHARACTERS_MAX - 7,
7373
}),
7474
).toBeDefined();
7575
expect(
7676
buildSlackDataTableBlock(base, {
77-
cellCharacterCountOffset: SLACK_DATA_TABLE_CELL_CHARACTERS_MAX - 6,
77+
cellCharacterCountOffset: SLACK_DATA_TABLE_AGGREGATE_CELL_CHARACTERS_MAX - 6,
7878
}),
7979
).toBeUndefined();
8080
});

0 commit comments

Comments
 (0)