Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-dm-thread-ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chat-adapter/slack": patch
---

Fix DM messages failing with `invalid_thread_ts` by guarding Slack API calls with `threadTs || undefined`
64 changes: 61 additions & 3 deletions packages/adapter-slack/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ describe("postMessage", () => {
expect(client.chat.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
channel: "C123",
thread_ts: "",
thread_ts: undefined,
})
);
});
Expand Down Expand Up @@ -2349,7 +2349,7 @@ describe("postEphemeral", () => {
);
});

it("omits thread_ts when empty", async () => {
it("normalizes empty threadTs to undefined", async () => {
const adapter = createSlackAdapter({
botToken: "xoxb-test-token",
signingSecret: secret,
Expand Down Expand Up @@ -3303,7 +3303,7 @@ describe("postChannelMessage", () => {
expect(client.chat.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
channel: "C123",
thread_ts: "",
thread_ts: undefined,
})
);
});
Expand Down Expand Up @@ -4982,3 +4982,61 @@ describe("reverse user lookup", () => {
});
});
});

// ============================================================================
// Empty threadTs normalization tests
// ============================================================================

describe("stream with empty threadTs", () => {
it("throws ValidationError when threadTs is empty", async () => {
const adapter = createSlackAdapter({
botToken: "xoxb-test-token",
signingSecret: "test-signing-secret",
logger: mockLogger,
});

async function* emptyStream() {
yield "hello";
}

await expect(
adapter.stream("slack:C123:", emptyStream(), {
recipientUserId: "U123",
recipientTeamId: "T123",
})
).rejects.toThrow(ValidationError);
});
});

describe("scheduleMessage with empty threadTs", () => {
it("normalizes empty threadTs to undefined", async () => {
const adapter = createSlackAdapter({
botToken: "xoxb-test-token",
signingSecret: "test-signing-secret",
logger: mockLogger,
});

mockClientMethod(
adapter,
"chat.scheduleMessage",
vi.fn().mockResolvedValue({
ok: true,
scheduled_message_id: "Q123",
post_at: Math.floor(Date.now() / 1000) + 3600,
})
);

const futureDate = new Date(Date.now() + 3600 * 1000);
await adapter.scheduleMessage("slack:C123:", "Scheduled msg", {
postAt: futureDate,
});

const client = getClient(adapter);
expect(client.chat.scheduleMessage).toHaveBeenCalledWith(
expect.objectContaining({
channel: "C123",
thread_ts: undefined,
})
);
});
});
33 changes: 23 additions & 10 deletions packages/adapter-slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2161,14 +2161,16 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
_message: AdapterPostableMessage
): Promise<RawMessage<unknown>> {
const message = await this.resolveMessageMentions(_message, threadId);
const { channel, threadTs } = this.decodeThreadId(threadId);
const { channel, threadTs: rawThreadTs } = this.decodeThreadId(threadId);
// Normalize empty threadTs to undefined to avoid Slack API "invalid_thread_ts" errors
const threadTs = rawThreadTs || undefined;

try {
// Check for files to upload
const files = extractFiles(message);
if (files.length > 0) {
// Upload files first (they're shared to the channel automatically)
await this.uploadFiles(files, channel, threadTs || undefined);
await this.uploadFiles(files, channel, threadTs);

// If message only has files (no text/card), return early
const hasText =
Expand Down Expand Up @@ -2302,7 +2304,8 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
_message: AdapterPostableMessage
): Promise<EphemeralMessage> {
const message = await this.resolveMessageMentions(_message, threadId);
const { channel, threadTs } = this.decodeThreadId(threadId);
const { channel, threadTs: rawThreadTs } = this.decodeThreadId(threadId);
const threadTs = rawThreadTs || undefined;

try {
// Check if message contains a card
Expand All @@ -2323,7 +2326,7 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
const result = await this.client.chat.postEphemeral(
this.withToken({
channel,
thread_ts: threadTs || undefined,
thread_ts: threadTs,
user: userId,
text: fallbackText,
blocks,
Expand Down Expand Up @@ -2356,7 +2359,7 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
const result = await this.client.chat.postEphemeral(
this.withToken({
channel,
thread_ts: threadTs || undefined,
thread_ts: threadTs,
user: userId,
text: tableResult.text,
blocks: tableResult.blocks,
Expand Down Expand Up @@ -2392,7 +2395,7 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
const result = await this.client.chat.postEphemeral(
this.withToken({
channel,
thread_ts: threadTs || undefined,
thread_ts: threadTs,
user: userId,
text,
})
Expand Down Expand Up @@ -2420,7 +2423,8 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
options: { postAt: Date }
): Promise<ScheduledMessage> {
const message = await this.resolveMessageMentions(_message, threadId);
const { channel, threadTs } = this.decodeThreadId(threadId);
const { channel, threadTs: rawThreadTs } = this.decodeThreadId(threadId);
const threadTs = rawThreadTs || undefined;
const postAtUnix = Math.floor(options.postAt.getTime() / 1000);

if (postAtUnix <= Math.floor(Date.now() / 1000)) {
Expand Down Expand Up @@ -2456,7 +2460,7 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
const result = await this.client.chat.scheduleMessage({
token,
channel,
thread_ts: threadTs || undefined,
thread_ts: threadTs,
post_at: postAtUnix,
text: fallbackText,
blocks,
Expand Down Expand Up @@ -2498,7 +2502,7 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
const result = await this.client.chat.scheduleMessage({
token,
channel,
thread_ts: threadTs || undefined,
thread_ts: threadTs,
post_at: postAtUnix,
text,
unfurl_links: false,
Expand Down Expand Up @@ -2929,7 +2933,16 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
"Slack streaming requires recipientUserId and recipientTeamId in options"
);
}
const { channel, threadTs } = this.decodeThreadId(threadId);
const { channel, threadTs: rawThreadTs } = this.decodeThreadId(threadId);
// Normalize empty threadTs to undefined to avoid Slack API "invalid_thread_ts" errors
const threadTs = rawThreadTs || undefined;
if (!threadTs) {
this.logger.debug("Slack: stream skipped - no thread context");
throw new ValidationError(
"slack",
"Slack streaming requires a valid thread context (non-empty threadTs)"
);
}
this.logger.debug("Slack: starting stream", { channel, threadTs });

const token = this.getToken();
Expand Down