fix: keep Slack thread replies in thread

This commit is contained in:
Peter Steinberger
2026-01-06 01:09:25 +01:00
parent 291c6f3b60
commit 5356adba8f
4 changed files with 46 additions and 9 deletions

View File

@@ -122,4 +122,38 @@ describe("monitorSlackProvider tool results", () => {
expect(sendMock.mock.calls[0][1]).toBe("PFX tool update");
expect(sendMock.mock.calls[1][1]).toBe("PFX final reply");
});
it("threads replies when incoming message is in a thread", async () => {
replyMock.mockResolvedValue({ text: "thread reply" });
const controller = new AbortController();
const run = monitorSlackProvider({
botToken: "bot-token",
appToken: "app-token",
abortSignal: controller.signal,
});
await waitForEvent("message");
const handler = getSlackHandlers()?.get("message");
if (!handler) throw new Error("Slack message handler not registered");
await handler({
event: {
type: "message",
user: "U1",
text: "hello",
ts: "123",
thread_ts: "456",
channel: "C1",
channel_type: "im",
},
});
await flush();
controller.abort();
await run;
expect(sendMock).toHaveBeenCalledTimes(1);
expect(sendMock.mock.calls[0][2]).toMatchObject({ threadTs: "456" });
});
});

View File

@@ -700,6 +700,9 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
);
}
// Only thread replies if the incoming message was in a thread.
const incomingThreadTs = message.thread_ts;
const dispatcher = createReplyDispatcher({
responsePrefix: cfg.messages?.responsePrefix,
deliver: async (payload) => {
@@ -709,6 +712,7 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
token: botToken,
runtime,
textLimit,
threadTs: incomingThreadTs,
});
},
onError: (err, info) => {
@@ -1379,6 +1383,7 @@ async function deliverReplies(params: {
token: string;
runtime: RuntimeEnv;
textLimit: number;
threadTs?: string;
}) {
const chunkLimit = Math.min(params.textLimit, 4000);
for (const payload of params.replies) {
@@ -1389,12 +1394,11 @@ async function deliverReplies(params: {
if (mediaList.length === 0) {
for (const chunk of chunkText(text, chunkLimit)) {
const threadTs = undefined;
const trimmed = chunk.trim();
if (!trimmed || trimmed === SILENT_REPLY_TOKEN) continue;
await sendMessageSlack(params.target, trimmed, {
token: params.token,
threadTs,
threadTs: params.threadTs,
});
}
} else {
@@ -1402,11 +1406,10 @@ async function deliverReplies(params: {
for (const mediaUrl of mediaList) {
const caption = first ? text : "";
first = false;
const threadTs = undefined;
await sendMessageSlack(params.target, caption, {
token: params.token,
mediaUrl,
threadTs,
threadTs: params.threadTs,
});
}
}