Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(slack): show postEphemeral on top-level mentions (#5014)

roboeve[bot]02bb7c4ee24f
diff --git a/.changeset/slack-ephemeral-top-level.md b/.changeset/slack-ephemeral-top-level.md
new file mode 100644
index 000000000..1d5a0d014
--- /dev/null
+++ b/.changeset/slack-ephemeral-top-level.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+`ctx.thread.postEphemeral()` on a fresh top-level Slack message now posts to the channel instead of an empty thread, where Slack hid it. Replies inside an existing thread stay threaded.
diff --git a/packages/eve/src/public/channels/slack/api.test.ts b/packages/eve/src/public/channels/slack/api.test.ts
index c4d0af8f1..8542edc36 100644
--- a/packages/eve/src/public/channels/slack/api.test.ts
+++ b/packages/eve/src/public/channels/slack/api.test.ts
@@ -780,6 +780,44 @@ describe("SlackThread.postEphemeral", () => {
expect(body.thread_ts).toBe("1.0");
expect(body.text).toBe("psst");
});
+
+ function ephemeralThreadTs(): (string | undefined)[] {
+ return mock.calls
+ .filter((c) => c.url === "https://slack.com/api/chat.postEphemeral")
+ .map((c) => (c.body as { thread_ts?: string }).thread_ts);
+ }
+
+ it("threads under a message without replies only after replying to it", async () => {
+ const { thread } = buildSlackBinding({
+ botToken: "xoxb-test",
+ channelId: "C01",
+ threadTs: "1.0",
+ threadHasReplies: false,
+ teamId: undefined,
+ });
+
+ await thread.postEphemeral("U99", "before");
+ await thread.post("reply");
+ await thread.postEphemeral("U99", "after");
+
+ expect(ephemeralThreadTs()).toEqual([undefined, "1.0"]);
+ });
+
+ it("does not thread under its own top-level anchor post", async () => {
+ const { thread, slack } = buildSlackBinding({
+ botToken: "xoxb-test",
+ channelId: "C01",
+ threadTs: "",
+ teamId: undefined,
+ });
+
+ await thread.post("anchor");
+ await thread.postEphemeral("U99", "before");
+ await thread.post("reply");
+ await thread.postEphemeral("U99", "after");
+
+ expect(ephemeralThreadTs()).toEqual([undefined, slack.threadTs]);
+ });
});
describe("SlackThread.postDirectMessage", () => {
diff --git a/packages/eve/src/public/channels/slack/api.ts b/packages/eve/src/public/channels/slack/api.ts
index ffd2398fc..b7c7a9622 100644
--- a/packages/eve/src/public/channels/slack/api.ts
+++ b/packages/eve/src/public/channels/slack/api.ts
@@ -267,7 +267,9 @@ export interface SlackThread {
/**
* Post an ephemeral reply (Slack's `chat.postEphemeral`) visible only
- * to one user in this thread. Accepts the same bare forms and
+ * to one user in this thread. Slack hides threaded ephemerals until the
+ * thread has a reply, so on a fresh top-level message it posts to the
+ * channel instead. Accepts the same bare forms and
* {@link SlackPostInput} variants as {@link post}. The `files` field is
* ignored: Slack does not support file uploads on ephemeral messages.
*/
@@ -438,6 +440,11 @@ export function buildSlackBinding(input: {
readonly botUserId?: string;
readonly channelId: string;
readonly threadTs: string;
+ /**
+ * Whether `threadTs` already has replies; Slack hides threaded
+ * ephemerals until it does. Defaults to true when `threadTs` is set.
+ */
+ readonly threadHasReplies?: boolean;
/** Workspace whose app installation supplies the bot token. */
readonly installationTeamId?: string;
/** Actor/content workspace exposed on the public handle. */
@@ -450,6 +457,7 @@ export function buildSlackBinding(input: {
const request = createSlackRequester(input.api, input.botToken, context);
let messages: readonly SlackThreadMessage[] = [];
let currentThreadTs = input.threadTs;
+ let threadHasReplies = input.threadHasReplies ?? currentThreadTs !== "";
let refreshInFlight: Promise<void> | undefined;
function handleMessageTs(ts: string): void {
@@ -566,6 +574,7 @@ export function buildSlackBinding(input: {
if (files.length > 0 && !shouldPostBeforeFiles) {
const comment = "text" in message ? message.text : undefined;
const result = await uploadFiles(files, { initialComment: comment });
+ if (currentThreadTs) threadHasReplies = true;
const id =
Array.isArray(result.raw.files) && result.raw.files.length > 0
? String((result.raw.files[0] as { id?: unknown }).id ?? "")
@@ -577,6 +586,7 @@ export function buildSlackBinding(input: {
buildPostMessageOptions(message, input.channelId, currentThreadTs, apiOptions),
);
const id = response.id;
+ if (currentThreadTs) threadHasReplies = true;
handleMessageTs(id);
// markdown / blocks / card + files: message lands first, then
@@ -592,8 +602,9 @@ export function buildSlackBinding(input: {
},
async postEphemeral(userId, rawMessage) {
const message = normalizePostInput(rawMessage);
+ const threadTs = threadHasReplies ? currentThreadTs : "";
const response = await postSlackEphemeral({
- ...buildPostMessageOptions(message, input.channelId, currentThreadTs, apiOptions),
+ ...buildPostMessageOptions(message, input.channelId, threadTs, apiOptions),
user: userId,
});
return { id: response.id, raw: response.raw };
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index e690c62d3..1f69990ec 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -2667,7 +2667,7 @@ describe("slackChannel() inbound mention pipeline", () => {
});
// #5012: Slack only shows a threaded ephemeral once the thread has a reply.
- it.fails("omits thread_ts from postEphemeral for a top-level mention", async () => {
+ it("omits thread_ts from postEphemeral for a top-level mention", async () => {
const channel = slackChannel({
credentials: { botToken: "xoxb-test" },
async onAppMention(ctx) {
diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.ts
index 54cd708db..92bcfe75c 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.ts
@@ -1232,6 +1232,7 @@ async function dispatchSlackMessage(input: {
botUserId: input.botUserId,
channelId: input.message.channelId,
threadTs: input.message.threadTs,
+ threadHasReplies: input.message.threadTs !== input.message.ts,
installationTeamId: input.installationTeamId,
teamId: input.message.teamId,
});