Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

feat(eve): add Slack private conversation helper (#2230)

Casey Gowrie7995d5701103
diff --git a/.changeset/slack-private-conversation-helper.md b/.changeset/slack-private-conversation-helper.md
new file mode 100644
index 000000000..40bd5307e
--- /dev/null
+++ b/.changeset/slack-private-conversation-helper.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Add `ctx.isDMOrPrivateChannel()` to Slack message handlers so agents can detect DMs, group DMs, and private channels without parsing raw events or implementing their own Slack API fallback.
diff --git a/docs/channels/slack.mdx b/docs/channels/slack.mdx
index ac1e6731c..4b4ce387e 100644
--- a/docs/channels/slack.mdx
+++ b/docs/channels/slack.mdx
@@ -198,15 +198,14 @@ export default slackChannel({
credentials: connectSlackCredentials("slack/my-agent"),
async onMessage(ctx, message) {
if (message.author?.isBot) return null;
- const isDirectMessage = message.raw.channel_type === "im";
- return isDirectMessage || ctx.isBotMentioned() || (await ctx.isSubscribed())
+ return (await ctx.isDMOrPrivateChannel()) || ctx.isBotMentioned() || (await ctx.isSubscribed())
? { auth: null }
: null;
},
});
```
-`isBotMentioned()` identifies an explicit mention. `isSubscribed()` checks whether the message belongs to a thread with an active eve session. For Vercel Connect, open **Advanced** when creating the connector and add `message.channels` under **Trigger Event Types** and `channels:history` under **Bot Scopes**. Private channels additionally need `message.groups` and `groups:history`.
+`isDMOrPrivateChannel()` returns `true` for DMs, group DMs, and private channels. Slack message events identify public channels as `channel` and private channels as `group`, so those events do not require an API lookup. Events such as `app_mention` omit the conversation type; for those, the helper calls Slack's `conversations.info` API and checks `is_private`. Missing scopes, API errors, and ambiguous responses return `true` so privacy-sensitive behavior fails closed. `isBotMentioned()` identifies an explicit mention. `isSubscribed()` checks whether the message belongs to a thread with an active eve session. For Vercel Connect, open **Advanced** when creating the connector and add `message.channels` under **Trigger Event Types** and `channels:history` under **Bot Scopes**. Private channels additionally need `message.groups` and `groups:history`.
Use `ctx.thread.listParticipants()` when routing depends on who has joined the thread. It fetches the current thread and returns unique human Slack user ids in first-appearance order, so the first id is the starting author for a human-started thread. Bot and system messages are excluded:
diff --git a/packages/eve/src/public/channels/slack/privacy.test.ts b/packages/eve/src/public/channels/slack/privacy.test.ts
new file mode 100644
index 000000000..629678881
--- /dev/null
+++ b/packages/eve/src/public/channels/slack/privacy.test.ts
@@ -0,0 +1,73 @@
+import { describe, expect, it, vi } from "vitest";
+
+import {
+ isPrivateSlackConversation,
+ readSlackConversationPrivacy,
+} from "#public/channels/slack/privacy.js";
+
+describe("Slack conversation privacy", () => {
+ it("reads explicit conversation types without an API lookup", async () => {
+ const request = vi.fn();
+
+ expect(readSlackConversationPrivacy({ channel_type: "channel" })).toBe("public");
+ expect(readSlackConversationPrivacy({ channel_type: "im" })).toBe("private");
+ expect(readSlackConversationPrivacy({ channel_type: "mpim" })).toBe("private");
+ expect(readSlackConversationPrivacy({ channel_type: "group" })).toBe("private");
+ expect(readSlackConversationPrivacy({})).toBe("unknown");
+ await expect(
+ isPrivateSlackConversation({
+ channelId: "C01",
+ raw: { channel_type: "channel" },
+ request,
+ }),
+ ).resolves.toBe(false);
+ await expect(
+ isPrivateSlackConversation({ channelId: "D01", raw: { channel_type: "im" }, request }),
+ ).resolves.toBe(true);
+ expect(request).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ { isPrivate: false, expected: false },
+ { isPrivate: true, expected: true },
+ ])(
+ "resolves events without channel_type through conversations.info when is_private is $isPrivate",
+ async ({ isPrivate, expected }) => {
+ const request = vi.fn().mockResolvedValue({
+ ok: true,
+ channel: { is_private: isPrivate },
+ });
+
+ await expect(
+ isPrivateSlackConversation({ channelId: "C01", raw: {}, request }),
+ ).resolves.toBe(expected);
+ expect(request).toHaveBeenCalledWith("conversations.info", { channel: "C01" });
+ },
+ );
+
+ it.each([
+ { response: { ok: true, channel: { is_private: true } }, label: "private channel" },
+ { response: { ok: true, channel: { is_im: true } }, label: "DM" },
+ { response: { ok: true, channel: { is_mpim: true } }, label: "group DM" },
+ { response: { ok: true, channel: {} }, label: "ambiguous response" },
+ { response: { ok: false, error: "missing_scope" }, label: "failed response" },
+ ])("treats a $label lookup as private", async ({ response }) => {
+ await expect(
+ isPrivateSlackConversation({
+ channelId: "C01",
+ raw: {},
+ request: vi.fn().mockResolvedValue(response),
+ }),
+ ).resolves.toBe(true);
+ });
+
+ it("fails closed when conversations.info throws", async () => {
+ await expect(
+ isPrivateSlackConversation({
+ channelId: "C01",
+ raw: {},
+ request: vi.fn().mockRejectedValue(new Error("missing_scope")),
+ }),
+ ).resolves.toBe(true);
+ });
+});
diff --git a/packages/eve/src/public/channels/slack/privacy.ts b/packages/eve/src/public/channels/slack/privacy.ts
new file mode 100644
index 000000000..a1f272cf5
--- /dev/null
+++ b/packages/eve/src/public/channels/slack/privacy.ts
@@ -0,0 +1,50 @@
+import type { SlackApiResponse } from "#public/channels/slack/api.js";
+
+export type SlackConversationPrivacy = "private" | "public" | "unknown";
+
+export function readSlackConversationPrivacy(
+ raw: Readonly<Record<string, unknown>> | undefined,
+): SlackConversationPrivacy {
+ switch (raw?.channel_type) {
+ case "im":
+ case "mpim":
+ case "group":
+ return "private";
+ case "channel":
+ return "public";
+ default:
+ return "unknown";
+ }
+}
+
+export async function isPrivateSlackConversation(input: {
+ readonly channelId: string;
+ readonly raw: Readonly<Record<string, unknown>> | undefined;
+ readonly request: (operation: string, body: unknown) => Promise<SlackApiResponse>;
+}): Promise<boolean> {
+ const eventPrivacy = readSlackConversationPrivacy(input.raw);
+ if (eventPrivacy !== "unknown") return eventPrivacy === "private";
+
+ try {
+ const privacy = readConversationInfoPrivacy(
+ await input.request("conversations.info", { channel: input.channelId }),
+ );
+ return privacy === "unknown" || privacy === "private";
+ } catch {
+ return true;
+ }
+}
+
+function readConversationInfoPrivacy(response: SlackApiResponse): SlackConversationPrivacy {
+ if (response.ok !== true || !isRecord(response.channel)) return "unknown";
+
+ const channel = response.channel;
+ if (channel.is_im === true || channel.is_mpim === true || channel.is_private === true) {
+ return "private";
+ }
+ return channel.is_private === false ? "public" : "unknown";
+}
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && !Array.isArray(value);
+}
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index 2a0a42f2e..d032085f0 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -1236,6 +1236,32 @@ describe("slackChannel() inbound mention pipeline", () => {
});
});
+ it("exposes private conversation detection to message handlers", async () => {
+ fetchMock.mockResolvedValue(
+ new Response(JSON.stringify({ ok: true, channel: { is_private: false } }), {
+ headers: { "content-type": "application/json" },
+ }),
+ );
+ let isPrivate: boolean | undefined;
+ const channel = slackChannel({
+ credentials: { botToken: "xoxb-test" },
+ async onAppMention(ctx) {
+ isPrivate = await ctx.isDMOrPrivateChannel();
+ return null;
+ },
+ });
+
+ const { body } = buildMentionBody({ channel: "C_PUBLIC" });
+ await firePost(channel, buildSignedRequest({ body }));
+
+ expect(isPrivate).toBe(false);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(String(fetchMock.mock.calls[0]![0])).toContain("conversations.info");
+ expect(parseSlackRequestBody(fetchMock.mock.calls[0]![1] as RequestInit)).toEqual({
+ channel: "C_PUBLIC",
+ });
+ });
+
it("binds onAppMention to flat session operations", async () => {
const clear = vi.fn().mockResolvedValue({ sessionId: "s1", status: "accepted" });
const channel = slackChannel({
diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.ts
index 27422c5ab..737d8ba39 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.ts
@@ -50,6 +50,7 @@ import {
formatSlackInboundMessage,
formatSlackThreadContext,
} from "#public/channels/slack/model-context.js";
+import { isPrivateSlackConversation } from "#public/channels/slack/privacy.js";
import {
loadThreadContextMessages,
type LoadThreadContextMessagesOptions,
@@ -370,6 +371,11 @@ export interface SlackInboundEventContext {
* `onDirectMessage`.
*/
export interface SlackInboundMessageContext extends SlackContext, SlackSessionOperations {
+ /**
+ * Returns whether the inbound message belongs to a DM, group DM, or private channel.
+ * Unknown conversation types fail closed and return `true`.
+ */
+ isDMOrPrivateChannel(): Promise<boolean>;
/** Returns whether this message belongs to a thread with an active eve session. */
isSubscribed(): Promise<boolean>;
/** Returns whether the inbound event explicitly mentions this bot. */
@@ -1130,6 +1136,12 @@ async function dispatchSlackMessage(input: {
isBotMentioned: () =>
input.kind === "app_mention" ||
(input.botUserId !== undefined && input.message.text.includes(`<@${input.botUserId}`)),
+ isDMOrPrivateChannel: () =>
+ isPrivateSlackConversation({
+ channelId: input.message.channelId,
+ raw: input.message.raw,
+ request: slack.request,
+ }),
isSubscribed: async () => (await sessionOperations.resolveSession()) !== undefined,
slack,
thread,