Skip to content

Commit 4669f05

Browse files
committed
fix(msteams): respect thread context in proactive send path
The proactive send path in send.ts hardcodes replyStyle: "top-level", causing all proactive/CLI messages to land as new top-level channel posts regardless of thread context. Additionally, messenger.ts throws when replyStyle is "thread" but no turn context is available. Two changes: 1. send.ts: resolve replyStyle from conversation context (channel + thread root → "thread", otherwise → "top-level") 2. messenger.ts: fall back to sendProactively with thread targeting instead of throwing when no turn context is available Fixes #78298
1 parent 6febffb commit 4669f05

4 files changed

Lines changed: 207 additions & 2 deletions

File tree

‎extensions/msteams/src/messenger.test.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,100 @@ describe("msteams messenger", () => {
574574
expect(reference.conversation?.id).toBe("19:[email protected];messageid=legacy-activity-id");
575575
});
576576

577+
it("falls back to proactive send with thread targeting when no context is provided", async () => {
578+
const proactiveSent: string[] = [];
579+
let capturedReference: unknown;
580+
581+
const channelRef: StoredConversationReference = {
582+
activityId: "thread-root-msg",
583+
threadId: "thread-root-msg",
584+
user: { id: "user123", name: "User" },
585+
agent: { id: "bot123", name: "Bot" },
586+
conversation: {
587+
588+
conversationType: "channel",
589+
},
590+
channelId: "msteams",
591+
serviceUrl: "https://service.example.com",
592+
};
593+
594+
const adapter: MSTeamsAdapter = {
595+
continueConversation: async (_appId, reference, logic) => {
596+
capturedReference = reference;
597+
await logic({
598+
sendActivity: createRecordedSendActivity(proactiveSent),
599+
updateActivity: noopUpdateActivity,
600+
deleteActivity: noopDeleteActivity,
601+
});
602+
},
603+
process: async () => {},
604+
updateActivity: noopUpdateActivity,
605+
deleteActivity: noopDeleteActivity,
606+
};
607+
608+
// No context provided — simulates the proactive send path
609+
const ids = await sendMSTeamsMessages({
610+
replyStyle: "thread",
611+
adapter,
612+
appId: "app123",
613+
conversationRef: channelRef,
614+
messages: [{ text: "proactive thread reply" }],
615+
});
616+
617+
// Message was delivered via proactive path
618+
expect(proactiveSent).toEqual(["proactive thread reply"]);
619+
expect(ids).toEqual(["id:proactive thread reply"]);
620+
621+
// The conversation reference includes the thread suffix
622+
const ref = capturedReference as { conversation?: { id?: string } };
623+
expect(ref.conversation?.id).toBe("19:[email protected];messageid=thread-root-msg");
624+
});
625+
626+
it("falls back to proactive send using activityId when threadId is absent and no context", async () => {
627+
const proactiveSent: string[] = [];
628+
let capturedReference: unknown;
629+
630+
const channelRef: StoredConversationReference = {
631+
activityId: "legacy-activity-id",
632+
// No threadId — uses activityId fallback
633+
user: { id: "user123", name: "User" },
634+
agent: { id: "bot123", name: "Bot" },
635+
conversation: {
636+
637+
conversationType: "channel",
638+
},
639+
channelId: "msteams",
640+
serviceUrl: "https://service.example.com",
641+
};
642+
643+
const adapter: MSTeamsAdapter = {
644+
continueConversation: async (_appId, reference, logic) => {
645+
capturedReference = reference;
646+
await logic({
647+
sendActivity: createRecordedSendActivity(proactiveSent),
648+
updateActivity: noopUpdateActivity,
649+
deleteActivity: noopDeleteActivity,
650+
});
651+
},
652+
process: async () => {},
653+
updateActivity: noopUpdateActivity,
654+
deleteActivity: noopDeleteActivity,
655+
};
656+
657+
const ids = await sendMSTeamsMessages({
658+
replyStyle: "thread",
659+
adapter,
660+
appId: "app123",
661+
conversationRef: channelRef,
662+
messages: [{ text: "fallback reply" }],
663+
});
664+
665+
expect(proactiveSent).toEqual(["fallback reply"]);
666+
expect(ids).toEqual(["id:fallback reply"]);
667+
const ref = capturedReference as { conversation?: { id?: string } };
668+
expect(ref.conversation?.id).toBe("19:[email protected];messageid=legacy-activity-id");
669+
});
670+
577671
it("does not add thread suffix for top-level replyStyle even with threadId set", async () => {
578672
let capturedReference: unknown;
579673
const sent: string[] = [];

‎extensions/msteams/src/messenger.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,11 @@ export async function sendMSTeamsMessages(params: {
572572
if (params.replyStyle === "thread") {
573573
const ctx = params.context;
574574
if (!ctx) {
575-
throw new Error("Missing context for replyStyle=thread");
575+
// When no turn context is available (proactive send path / CLI sends),
576+
// fall back to sendProactively with thread targeting instead of throwing.
577+
// The proactive path reconstructs the threaded conversation ID
578+
// (;messageid=<threadRootId>) so the message lands in the correct thread.
579+
return await sendProactively(messages, 0, resolvedThreadId);
576580
}
577581
const messageIds: string[] = [];
578582
for (const [idx, message] of messages.entries()) {

‎extensions/msteams/src/send.test.ts‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,106 @@ describe("sendMessageMSTeams", () => {
302302
);
303303
});
304304

305+
it("passes replyStyle thread for channel conversations with threadId", async () => {
306+
mockState.resolveMSTeamsSendContext.mockResolvedValue({
307+
adapter: {},
308+
appId: "app-id",
309+
conversationId: "19:[email protected]",
310+
ref: {
311+
threadId: "thread-root-123",
312+
user: { id: "user-1" },
313+
agent: { id: "agent-1" },
314+
conversation: { id: "19:[email protected]", conversationType: "channel" },
315+
channelId: "msteams",
316+
},
317+
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
318+
conversationType: "channel",
319+
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
320+
mediaMaxBytes: 8 * 1024,
321+
});
322+
323+
await sendMessageMSTeams({
324+
cfg: {} as OpenClawConfig,
325+
to: "conversation:19:[email protected]",
326+
text: "threaded reply",
327+
});
328+
329+
expect(mockState.sendMSTeamsMessages).toHaveBeenCalledWith(
330+
expect.objectContaining({ replyStyle: "thread" }),
331+
);
332+
});
333+
334+
it("passes replyStyle thread for channel conversations with activityId fallback", async () => {
335+
mockState.resolveMSTeamsSendContext.mockResolvedValue({
336+
adapter: {},
337+
appId: "app-id",
338+
conversationId: "19:[email protected]",
339+
ref: {
340+
activityId: "activity-root-456",
341+
user: { id: "user-1" },
342+
agent: { id: "agent-1" },
343+
conversation: { id: "19:[email protected]", conversationType: "channel" },
344+
channelId: "msteams",
345+
},
346+
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
347+
conversationType: "channel",
348+
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
349+
mediaMaxBytes: 8 * 1024,
350+
});
351+
352+
await sendMessageMSTeams({
353+
cfg: {} as OpenClawConfig,
354+
to: "conversation:19:[email protected]",
355+
text: "threaded via activityId",
356+
});
357+
358+
expect(mockState.sendMSTeamsMessages).toHaveBeenCalledWith(
359+
expect.objectContaining({ replyStyle: "thread" }),
360+
);
361+
});
362+
363+
it("passes replyStyle top-level for personal conversations", async () => {
364+
// Default mock is personal — verify it stays top-level
365+
await sendMessageMSTeams({
366+
cfg: {} as OpenClawConfig,
367+
to: "conversation:19:[email protected]",
368+
text: "dm message",
369+
});
370+
371+
expect(mockState.sendMSTeamsMessages).toHaveBeenCalledWith(
372+
expect.objectContaining({ replyStyle: "top-level" }),
373+
);
374+
});
375+
376+
it("passes replyStyle top-level for channel conversations without thread root", async () => {
377+
mockState.resolveMSTeamsSendContext.mockResolvedValue({
378+
adapter: {},
379+
appId: "app-id",
380+
conversationId: "19:[email protected]",
381+
ref: {
382+
// No threadId, no activityId
383+
user: { id: "user-1" },
384+
agent: { id: "agent-1" },
385+
conversation: { id: "19:[email protected]", conversationType: "channel" },
386+
channelId: "msteams",
387+
},
388+
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
389+
conversationType: "channel",
390+
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
391+
mediaMaxBytes: 8 * 1024,
392+
});
393+
394+
await sendMessageMSTeams({
395+
cfg: {} as OpenClawConfig,
396+
to: "conversation:19:[email protected]",
397+
text: "no thread root",
398+
});
399+
400+
expect(mockState.sendMSTeamsMessages).toHaveBeenCalledWith(
401+
expect.objectContaining({ replyStyle: "top-level" }),
402+
);
403+
});
404+
305405
it("falls back to conversationId when graphChatId is not available", async () => {
306406
const botFrameworkConversationId = "19:[email protected]";
307407

‎extensions/msteams/src/send.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,17 @@ async function sendTextWithMedia(
393393
mediaMaxBytes,
394394
} = ctx;
395395

396+
// Resolve replyStyle from conversation context instead of hardcoding "top-level".
397+
// For channel conversations with a known thread root, use "thread" so the
398+
// proactive message lands in the correct thread. For DMs and group chats,
399+
// continue using "top-level".
400+
const isChannelThread = ctx.conversationType === "channel" && (ref.threadId || ref.activityId);
401+
const resolvedReplyStyle = isChannelThread ? "thread" : "top-level";
402+
396403
let platformMessageIds: string[];
397404
try {
398405
platformMessageIds = await sendMSTeamsMessages({
399-
replyStyle: "top-level",
406+
replyStyle: resolvedReplyStyle,
400407
adapter,
401408
appId,
402409
conversationRef: ref,

0 commit comments

Comments
 (0)