Slack: validate blocks input shape centrally

This commit is contained in:
Colin
2026-02-16 12:30:33 -05:00
committed by Peter Steinberger
parent e023c84d78
commit 10d876e319
6 changed files with 119 additions and 37 deletions

View File

@@ -193,6 +193,20 @@ describe("handleSlackAction", () => {
).rejects.toThrow(/blocks must be valid JSON/i);
});
it("rejects empty blocks arrays", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
await expect(
handleSlackAction(
{
action: "sendMessage",
to: "channel:C123",
blocks: "[]",
},
cfg,
),
).rejects.toThrow(/at least one block/i);
});
it("requires at least one of content, blocks, or mediaUrl", async () => {
const cfg = { channels: { slack: { botToken: "tok" } } } as OpenClawConfig;
await expect(

View File

@@ -1,5 +1,4 @@
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
import type { Block, KnownBlock } from "@slack/web-api";
import type { OpenClawConfig } from "../../config/config.js";
import { resolveSlackAccount } from "../../slack/accounts.js";
import {
@@ -17,6 +16,7 @@ import {
sendSlackMessage,
unpinSlackMessage,
} from "../../slack/actions.js";
import { parseSlackBlocksInput } from "../../slack/blocks-input.js";
import { parseSlackTarget, resolveSlackChannelId } from "../../slack/targets.js";
import { withNormalizedTimestamp } from "../date-time.js";
import {
@@ -86,24 +86,7 @@ function resolveThreadTsFromContext(
}
function readSlackBlocksParam(params: Record<string, unknown>) {
const raw = params.blocks;
if (raw == null) {
return undefined;
}
const parsed =
typeof raw === "string"
? (() => {
try {
return JSON.parse(raw);
} catch {
throw new Error("blocks must be valid JSON");
}
})()
: raw;
if (!Array.isArray(parsed)) {
throw new Error("blocks must be an array");
}
return parsed as (Block | KnownBlock)[];
return parseSlackBlocksInput(params.blocks);
}
export async function handleSlackAction(