mom: fix [SILENT] to delete thread messages too

Track all thread message timestamps during a run. When response is
[SILENT], delete all thread messages before deleting the main message.
This prevents the 'This message was deleted' tombstone in Slack.
This commit is contained in:
Mario Zechner 2025-12-12 22:56:56 +01:00
parent 8ba6aa6627
commit f1451fd8f0
3 changed files with 18 additions and 5 deletions

View file

@ -690,11 +690,11 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
.map((c) => c.text)
.join("\n") || "";
// Check for [SILENT] marker - delete message instead of posting
// Check for [SILENT] marker - delete message and thread instead of posting
if (finalText.trim() === "[SILENT]" || finalText.trim().startsWith("[SILENT]")) {
try {
await ctx.deleteMessage();
log.logInfo("Silent response - deleted status message");
log.logInfo("Silent response - deleted message and thread");
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err);
log.logWarning("Failed to delete message for silent response", errMsg);

View file

@ -116,6 +116,7 @@ function getState(channelId: string): ChannelState {
function createSlackContext(event: SlackEvent, slack: SlackBot, state: ChannelState, isEvent?: boolean) {
let messageTs: string | null = null;
const threadMessageTs: string[] = [];
let accumulatedText = "";
let isWorking = true;
const workingIndicator = " ...";
@ -175,7 +176,8 @@ function createSlackContext(event: SlackEvent, slack: SlackBot, state: ChannelSt
respondInThread: async (text: string) => {
updatePromise = updatePromise.then(async () => {
if (messageTs) {
await slack.postInThread(event.channel, messageTs, text);
const ts = await slack.postInThread(event.channel, messageTs, text);
threadMessageTs.push(ts);
}
});
await updatePromise;
@ -210,6 +212,16 @@ function createSlackContext(event: SlackEvent, slack: SlackBot, state: ChannelSt
deleteMessage: async () => {
updatePromise = updatePromise.then(async () => {
// Delete thread messages first (in reverse order)
for (let i = threadMessageTs.length - 1; i >= 0; i--) {
try {
await slack.deleteMessage(event.channel, threadMessageTs[i]);
} catch {
// Ignore errors deleting thread messages
}
}
threadMessageTs.length = 0;
// Then delete main message
if (messageTs) {
await slack.deleteMessage(event.channel, messageTs);
messageTs = null;

View file

@ -197,8 +197,9 @@ export class SlackBot {
await this.webClient.chat.delete({ channel, ts });
}
async postInThread(channel: string, threadTs: string, text: string): Promise<void> {
await this.webClient.chat.postMessage({ channel, thread_ts: threadTs, text });
async postInThread(channel: string, threadTs: string, text: string): Promise<string> {
const result = await this.webClient.chat.postMessage({ channel, thread_ts: threadTs, text });
return result.ts as string;
}
async uploadFile(channel: string, filePath: string, title?: string): Promise<void> {