Files
openclaw/extensions/slack/src/threading.ts
Yossi Eliaz 443255461c fix(slack): preserve assistant DM root thread context (#63840)
Preserve Slack Agents & Assistants DM root thread context for tool and subagent replies even when Slack omits or misreports `channel_type`, while leaving non-DM self-thread roots top-level.

Fixes #63659.

Thanks @zozo123.
2026-05-30 22:28:49 +01:00

66 lines
2.4 KiB
TypeScript

import type { ReplyToMode } from "openclaw/plugin-sdk/config-contracts";
import type { SlackAppMentionEvent, SlackMessageEvent } from "./types.js";
type SlackThreadContext = {
incomingThreadTs?: string;
messageTs?: string;
isThreadReply: boolean;
replyToId?: string;
messageThreadId?: string;
};
export function resolveSlackThreadContext(params: {
message: SlackMessageEvent | SlackAppMentionEvent;
replyToMode: ReplyToMode;
isDirectMessage?: boolean;
}): SlackThreadContext {
const incomingThreadTs = params.message.thread_ts;
const eventTs = params.message.event_ts;
const messageTs = params.message.ts ?? eventTs;
const hasThreadTs = typeof incomingThreadTs === "string" && incomingThreadTs.length > 0;
const isThreadReply =
hasThreadTs && (incomingThreadTs !== messageTs || Boolean(params.message.parent_user_id));
const replyToId = incomingThreadTs ?? messageTs;
// Preserve thread context for Slack Agents & Assistants DM root messages
// where thread_ts == ts. Non-DM self-thread roots must stay unset because
// downstream tool threading treats MessageThreadId as an explicit thread
// target and overrides replyToMode to "all".
const isAssistantDmThreadRoot = hasThreadTs && !isThreadReply && params.isDirectMessage === true;
const messageThreadId =
isThreadReply || isAssistantDmThreadRoot
? incomingThreadTs
: params.replyToMode === "all"
? messageTs
: undefined;
return {
incomingThreadTs,
messageTs,
isThreadReply,
replyToId,
messageThreadId,
};
}
/**
* Resolves Slack thread targeting for replies and status indicators.
*
* @returns replyThreadTs - Thread timestamp for reply messages
* @returns statusThreadTs - Thread timestamp for status indicators (typing, etc.)
* @returns isThreadReply - true if this is a genuine user reply in a thread,
* false if thread_ts comes from a bot status message (e.g. typing indicator)
*/
export function resolveSlackThreadTargets(params: {
message: SlackMessageEvent | SlackAppMentionEvent;
replyToMode: ReplyToMode;
}) {
const ctx = resolveSlackThreadContext(params);
const { incomingThreadTs, messageTs, isThreadReply } = ctx;
const replyThreadTs = isThreadReply
? incomingThreadTs
: params.replyToMode === "all"
? messageTs
: undefined;
const statusThreadTs = replyThreadTs;
return { replyThreadTs, statusThreadTs, isThreadReply };
}