feat(slack): add download-file action for on-demand file attachment access (#24723)

* feat(slack): add download-file action for on-demand file attachment access

Adds a new `download-file` message tool action that allows the agent to
download Slack file attachments by file ID on demand. This is a prerequisite
for accessing images posted in thread history, where file attachments are
not automatically resolved.

Changes:
- Add `files` field to `SlackMessageSummary` type so file IDs are
  visible in message read results
- Add `downloadSlackFile()` to fetch a file by ID via `files.info`
  and resolve it through the existing `resolveSlackMedia()` pipeline
- Register `download-file` in `CHANNEL_MESSAGE_ACTION_NAMES`,
  `MESSAGE_ACTION_TARGET_MODE`, and `listSlackMessageActions`
- Add `downloadFile` dispatch case in `handleSlackAction`
- Wire agent-facing `download-file` → internal `downloadFile` in
  `handleSlackMessageAction`

Closes #24681

* style: fix formatting in slack-actions and actions

* test(slack): cover download-file action path

---------

Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
msvechla
2026-03-01 18:45:05 +01:00
committed by GitHub
parent eddaf19478
commit 2c5b898eea
10 changed files with 256 additions and 1 deletions

View File

@@ -0,0 +1,88 @@
import type { WebClient } from "@slack/web-api";
import { describe, expect, it, vi } from "vitest";
const resolveSlackMedia = vi.fn();
vi.mock("./monitor/media.js", () => ({
resolveSlackMedia: (...args: Parameters<typeof resolveSlackMedia>) => resolveSlackMedia(...args),
}));
const { downloadSlackFile } = await import("./actions.js");
function createClient() {
return {
files: {
info: vi.fn(async () => ({ file: {} })),
},
} as unknown as WebClient & {
files: {
info: ReturnType<typeof vi.fn>;
};
};
}
describe("downloadSlackFile", () => {
it("returns null when files.info has no private download URL", async () => {
const client = createClient();
client.files.info.mockResolvedValueOnce({
file: {
id: "F123",
name: "image.png",
},
});
const result = await downloadSlackFile("F123", {
client,
token: "xoxb-test",
maxBytes: 1024,
});
expect(result).toBeNull();
expect(resolveSlackMedia).not.toHaveBeenCalled();
});
it("downloads via resolveSlackMedia using fresh files.info metadata", async () => {
const client = createClient();
client.files.info.mockResolvedValueOnce({
file: {
id: "F123",
name: "image.png",
mimetype: "image/png",
url_private_download: "https://files.slack.com/files-pri/T1-F123/image.png",
},
});
resolveSlackMedia.mockResolvedValueOnce([
{
path: "/tmp/image.png",
contentType: "image/png",
placeholder: "[Slack file: image.png]",
},
]);
const result = await downloadSlackFile("F123", {
client,
token: "xoxb-test",
maxBytes: 1024,
});
expect(client.files.info).toHaveBeenCalledWith({ file: "F123" });
expect(resolveSlackMedia).toHaveBeenCalledWith({
files: [
{
id: "F123",
name: "image.png",
mimetype: "image/png",
url_private: undefined,
url_private_download: "https://files.slack.com/files-pri/T1-F123/image.png",
},
],
token: "xoxb-test",
maxBytes: 1024,
});
expect(result).toEqual({
path: "/tmp/image.png",
contentType: "image/png",
placeholder: "[Slack file: image.png]",
});
});
});

View File

@@ -5,6 +5,8 @@ import { resolveSlackAccount } from "./accounts.js";
import { buildSlackBlocksFallbackText } from "./blocks-fallback.js";
import { validateSlackBlocksArray } from "./blocks-input.js";
import { createSlackWebClient } from "./client.js";
import { resolveSlackMedia } from "./monitor/media.js";
import type { SlackMediaResult } from "./monitor/media.js";
import { sendMessageSlack } from "./send.js";
import { resolveSlackBotToken } from "./token.js";
@@ -25,6 +27,12 @@ export type SlackMessageSummary = {
count?: number;
users?: string[];
}>;
/** File attachments on this message. Present when the message has files. */
files?: Array<{
id?: string;
name?: string;
mimetype?: string;
}>;
};
export type SlackPin = {
@@ -271,3 +279,48 @@ export async function listSlackPins(
const result = await client.pins.list({ channel: channelId });
return (result.items ?? []) as SlackPin[];
}
/**
* Downloads a Slack file by ID and saves it to the local media store.
* Fetches a fresh download URL via files.info to avoid using stale private URLs.
* Returns null when the file cannot be found or downloaded.
*/
export async function downloadSlackFile(
fileId: string,
opts: SlackActionClientOpts & { maxBytes: number },
): Promise<SlackMediaResult | null> {
const token = resolveToken(opts.token, opts.accountId);
const client = await getClient(opts);
// Fetch fresh file metadata (includes a current url_private_download).
const info = await client.files.info({ file: fileId });
const file = info.file as
| {
id?: string;
name?: string;
mimetype?: string;
url_private?: string;
url_private_download?: string;
}
| undefined;
if (!file?.url_private_download && !file?.url_private) {
return null;
}
const results = await resolveSlackMedia({
files: [
{
id: file.id,
name: file.name,
mimetype: file.mimetype,
url_private: file.url_private,
url_private_download: file.url_private_download,
},
],
token,
maxBytes: opts.maxBytes,
});
return results?.[0] ?? null;
}

View File

@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { listSlackMessageActions } from "./message-actions.js";
describe("listSlackMessageActions", () => {
it("includes download-file when message actions are enabled", () => {
const cfg = {
channels: {
slack: {
botToken: "xoxb-test",
actions: {
messages: true,
},
},
},
} as OpenClawConfig;
expect(listSlackMessageActions(cfg)).toEqual(
expect.arrayContaining(["read", "edit", "delete", "download-file"]),
);
});
});

View File

@@ -32,6 +32,7 @@ export function listSlackMessageActions(cfg: OpenClawConfig): ChannelMessageActi
actions.add("read");
actions.add("edit");
actions.add("delete");
actions.add("download-file");
}
if (isActionEnabled("pins")) {
actions.add("pin");