Skip to content

Commit 17625e5

Browse files
authored
fix(msteams): preserve thread targets for structured sends (#117516)
1 parent 29577fb commit 17625e5

6 files changed

Lines changed: 209 additions & 85 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,15 @@ describe("msteamsOutbound cfg threading", () => {
297297

298298
const result = await requireSendPayload()({
299299
cfg,
300-
to: "conversation:abc",
300+
to: "conversation:19:[email protected]",
301+
threadId: "presentation-thread-root",
301302
text: "Deploy finished",
302303
payload: rendered!,
303304
});
304305

305306
expect(mocks.sendAdaptiveCardMSTeams).toHaveBeenCalledWith({
306307
cfg,
307-
to: "conversation:abc",
308+
to: "conversation:19:[email protected];messageid=presentation-thread-root",
308309
card: (rendered!.channelData!.msteams as { presentationCard: unknown }).presentationCard,
309310
});
310311
expect(result).toEqual({
@@ -574,6 +575,26 @@ describe("msteamsOutbound cfg threading", () => {
574575
expect(Number.isNaN(Date.parse(pollRecord?.createdAt))).toBe(false);
575576
});
576577

578+
it("forwards resolved channel thread ids to poll sends", async () => {
579+
await requireSendPoll()({
580+
cfg,
581+
to: "conversation:19:[email protected]",
582+
threadId: "poll-thread-root",
583+
poll: {
584+
question: "Ship it?",
585+
options: ["Yes", "No"],
586+
},
587+
});
588+
589+
expect(mocks.sendPollMSTeams).toHaveBeenCalledWith({
590+
cfg,
591+
to: "conversation:19:[email protected];messageid=poll-thread-root",
592+
question: "Ship it?",
593+
options: ["Yes", "No"],
594+
maxSelections: 1,
595+
});
596+
});
597+
577598
it("chunks outbound text without requiring MSTeams runtime initialization", () => {
578599
const chunker = msteamsOutbound.chunker;
579600
if (!chunker) {

‎extensions/msteams/src/outbound.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ export const msteamsOutbound: ChannelOutboundAdapter = {
218218
mediaReadFile,
219219
});
220220
},
221-
sendPoll: async ({ cfg, to, poll }) => {
221+
sendPoll: async ({ cfg, to, poll, threadId }) => {
222222
const maxSelections = poll.maxSelections ?? 1;
223223
const result = await sendPollMSTeams({
224224
cfg,
225-
to,
225+
to: resolveMSTeamsThreadTarget(to, threadId),
226226
question: poll.question,
227227
options: poll.options,
228228
maxSelections,

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function channelRef(params?: Partial<StoredConversationReference>): StoredConver
5050
};
5151
}
5252

53-
async function resolveMSTeamsProactiveReplyStyle(params: {
53+
async function resolveMSTeamsProactiveReplyTarget(params: {
5454
cfg?: MSTeamsConfig;
5555
conversationId: string;
5656
ref: StoredConversationReference;
@@ -76,12 +76,14 @@ async function resolveMSTeamsProactiveReplyStyle(params: {
7676
},
7777
},
7878
} as OpenClawConfig;
79-
return (
80-
await resolveMSTeamsSendContext({
81-
cfg,
82-
to: `conversation:${params.conversationId}`,
83-
})
84-
).replyStyle;
79+
const context = await resolveMSTeamsSendContext({
80+
cfg,
81+
to: `conversation:${params.conversationId}`,
82+
});
83+
return {
84+
replyStyle: context.replyStyle,
85+
threadActivityId: context.threadActivityId,
86+
};
8587
}
8688

8789
beforeEach(() => {
@@ -155,6 +157,7 @@ describe("resolveMSTeamsSendContext", () => {
155157
conversationId: "19:[email protected]",
156158
ref: { threadId: "explicit-root" },
157159
replyStyle: "thread",
160+
threadActivityId: "explicit-root",
158161
});
159162
expect(sendContextMockState.store.get).toHaveBeenCalledWith("19:[email protected]");
160163
});
@@ -186,6 +189,7 @@ describe("resolveMSTeamsSendContext", () => {
186189
conversationId: "19:[email protected]",
187190
ref: { threadId: "graph-root" },
188191
replyStyle: "thread",
192+
threadActivityId: "graph-root",
189193
});
190194
expect(sendContextMockState.store.get).toHaveBeenCalledWith("19:[email protected]");
191195
});
@@ -248,27 +252,27 @@ describe("resolveMSTeamsSendContext", () => {
248252
});
249253
});
250254

251-
describe("resolveMSTeamsProactiveReplyStyle", () => {
255+
describe("resolveMSTeamsProactiveReplyTarget", () => {
252256
it("uses thread for channel conversations with a stored thread root", async () => {
253257
await expect(
254-
resolveMSTeamsProactiveReplyStyle({
258+
resolveMSTeamsProactiveReplyTarget({
255259
cfg: {},
256260
conversationId: "19:[email protected]",
257261
ref: channelRef({ threadId: "thread-root-1" }),
258262
conversationType: "channel",
259263
}),
260-
).resolves.toBe("thread");
264+
).resolves.toEqual({ replyStyle: "thread", threadActivityId: "thread-root-1" });
261265
});
262266

263267
it("falls back to activityId for legacy channel references", async () => {
264268
await expect(
265-
resolveMSTeamsProactiveReplyStyle({
269+
resolveMSTeamsProactiveReplyTarget({
266270
cfg: {},
267271
conversationId: "19:[email protected]",
268272
ref: channelRef({ activityId: "legacy-root-1" }),
269273
conversationType: "channel",
270274
}),
271-
).resolves.toBe("thread");
275+
).resolves.toEqual({ replyStyle: "thread", threadActivityId: "legacy-root-1" });
272276
});
273277

274278
it("keeps configured top-level channel routing", async () => {
@@ -284,44 +288,44 @@ describe("resolveMSTeamsProactiveReplyStyle", () => {
284288
};
285289

286290
await expect(
287-
resolveMSTeamsProactiveReplyStyle({
291+
resolveMSTeamsProactiveReplyTarget({
288292
cfg,
289293
conversationId: "19:[email protected]",
290294
ref: channelRef({ threadId: "thread-root-1" }),
291295
conversationType: "channel",
292296
}),
293-
).resolves.toBe("top-level");
297+
).resolves.toEqual({ replyStyle: "top-level", threadActivityId: undefined });
294298
});
295299

296300
it("uses top-level when a channel has no stored thread root", async () => {
297301
await expect(
298-
resolveMSTeamsProactiveReplyStyle({
302+
resolveMSTeamsProactiveReplyTarget({
299303
cfg: { replyStyle: "thread" },
300304
conversationId: "19:[email protected]",
301305
ref: channelRef(),
302306
conversationType: "channel",
303307
}),
304-
).resolves.toBe("top-level");
308+
).resolves.toEqual({ replyStyle: "top-level", threadActivityId: undefined });
305309
});
306310

307311
it("uses top-level for non-channel conversations", async () => {
308312
const ref = channelRef({ activityId: "activity-1" });
309313

310314
await expect(
311-
resolveMSTeamsProactiveReplyStyle({
315+
resolveMSTeamsProactiveReplyTarget({
312316
cfg: { replyStyle: "thread" },
313317
conversationId: "19:[email protected]",
314318
ref,
315319
conversationType: "groupChat",
316320
}),
317-
).resolves.toBe("top-level");
321+
).resolves.toEqual({ replyStyle: "top-level", threadActivityId: undefined });
318322
await expect(
319-
resolveMSTeamsProactiveReplyStyle({
323+
resolveMSTeamsProactiveReplyTarget({
320324
cfg: { replyStyle: "thread" },
321325
conversationId: "a:personal",
322326
ref,
323327
conversationType: "personal",
324328
}),
325-
).resolves.toBe("top-level");
329+
).resolves.toEqual({ replyStyle: "top-level", threadActivityId: undefined });
326330
});
327331
});

‎extensions/msteams/src/send-context.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coer
33
import {
44
resolveChannelMediaMaxBytes,
55
type MSTeamsConfig,
6-
type MSTeamsReplyStyle,
76
type OpenClawConfig,
87
type PluginRuntime,
98
} from "../runtime-api.js";
@@ -33,6 +32,12 @@ import { resolveMSTeamsCredentials } from "./token.js";
3332

3433
type MSTeamsConversationType = "personal" | "groupChat" | "channel";
3534

35+
// Keep reply policy and the Connector thread suffix together so every proactive
36+
// activity kind uses the same resolved destination instead of re-deriving it.
37+
type MSTeamsProactiveReplyTarget =
38+
| { replyStyle: "thread"; threadActivityId: string }
39+
| { replyStyle: "top-level"; threadActivityId?: never };
40+
3641
export type MSTeamsProactiveContext = {
3742
appId: string;
3843
conversationId: string;
@@ -41,8 +46,6 @@ export type MSTeamsProactiveContext = {
4146
log: ReturnType<PluginRuntime["logging"]["getChildLogger"]>;
4247
/** The type of conversation: personal (1:1), groupChat, or channel */
4348
conversationType: MSTeamsConversationType;
44-
/** Reply style resolved for proactive text/media sends. */
45-
replyStyle: MSTeamsReplyStyle;
4649
/** Teams SDK cloud/service endpoint used to validate proactive sends. */
4750
sdkCloudOptions: MSTeamsSdkCloudOptions;
4851
/** Token provider for Graph API / SharePoint operations */
@@ -51,17 +54,17 @@ export type MSTeamsProactiveContext = {
5154
sharePointSiteId?: string;
5255
/** Resolved media max bytes from config (default: 100MB) */
5356
mediaMaxBytes?: number;
54-
};
57+
} & MSTeamsProactiveReplyTarget;
5558

56-
function resolveMSTeamsProactiveReplyStyle(params: {
59+
function resolveMSTeamsProactiveReplyTarget(params: {
5760
cfg?: MSTeamsConfig;
5861
conversationId: string;
5962
ref: StoredConversationReference;
6063
conversationType: MSTeamsConversationType;
61-
}): MSTeamsReplyStyle {
64+
}): MSTeamsProactiveReplyTarget {
6265
const threadRootId = params.ref.threadId ?? params.ref.activityId;
6366
if (params.conversationType !== "channel" || !threadRootId) {
64-
return "top-level";
67+
return { replyStyle: "top-level" };
6568
}
6669

6770
const routeConfig = resolveMSTeamsRouteConfig({
@@ -76,7 +79,7 @@ function resolveMSTeamsProactiveReplyStyle(params: {
7679
teamConfig: routeConfig.teamConfig,
7780
channelConfig: routeConfig.channelConfig,
7881
});
79-
return replyStyle;
82+
return replyStyle === "thread" ? { replyStyle, threadActivityId: threadRootId } : { replyStyle };
8083
}
8184

8285
/**
@@ -250,10 +253,10 @@ export async function resolveMSTeamsSendContext(params: {
250253
// An explicit messageid is a caller-owned destination. Ambient and stored
251254
// roots still obey route policy, but explicit channel roots must not be
252255
// flattened by a top-level default.
253-
const replyStyle =
256+
const replyTarget: MSTeamsProactiveReplyTarget =
254257
recipient.threadId && conversationType === "channel"
255-
? "thread"
256-
: resolveMSTeamsProactiveReplyStyle({
258+
? { replyStyle: "thread", threadActivityId: recipient.threadId }
259+
: resolveMSTeamsProactiveReplyTarget({
257260
cfg: msteamsCfg,
258261
conversationId,
259262
ref: safeRef,
@@ -276,7 +279,7 @@ export async function resolveMSTeamsSendContext(params: {
276279
app,
277280
log,
278281
conversationType,
279-
replyStyle,
282+
...replyTarget,
280283
sdkCloudOptions,
281284
tokenProvider,
282285
sharePointSiteId,

0 commit comments

Comments
 (0)