Skip to content

Commit 93b8a7c

Browse files
committed
fix(msteams): preserve proactive thread replies
1 parent 34dc7f6 commit 93b8a7c

7 files changed

Lines changed: 281 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Docs: https://docs.openclaw.ai
116116
- PDF/Codex: include extraction-fallback instructions for `openai-codex/*` PDF tool requests so Codex Responses receives its required system prompt. Fixes #77872. Thanks @anyech.
117117
- Onboard/channels: recover externalized channel plugins from stale `channels.<id>` config by falling back to `ensureChannelSetupPluginInstalled` via the trusted catalog when the plugin is missing on disk, so leftover `appId`/token entries no longer dead-end onboard with "<channel> plugin not available." (#78328) Thanks @sliverp.
118118
- Codex/app-server: forward the OpenClaw workspace bootstrap block through Codex `developerInstructions` instead of `config.instructions`, so persona/style guidance reaches the behavior-shaping app-server lane. Fixes #77363. Thanks @lonexreb.
119+
- MS Teams: route proactive channel sends with stored thread roots through the configured threaded reply path instead of forcing every CLI/message-tool send into a new top-level post. Fixes #78298. Thanks @amknight.
119120
- CLI/infer: pass minimal instructions to local `openai-codex/*` model probes and surface provider error details when `infer model run` returns no text. Fixes #76464. Thanks @lilesjtu.
120121
- Dependencies: override transitive `ip-address` to `10.2.0` so the runtime lockfile no longer includes the vulnerable `10.1.0` build flagged by Dependabot alert 109. Thanks @vincentkoc.
121122
- Feishu: hydrate missing native topic starter thread IDs before session routing so first turns and follow-ups stay in the same topic session. Fixes #78262. Thanks @joeyzenghuan.

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

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

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

‎extensions/msteams/src/messenger.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ 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+
return await sendProactively(messages, 0, resolvedThreadId);
576576
}
577577
const messageIds: string[] = [];
578578
for (const [idx, message] of messages.entries()) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { MSTeamsConfig } from "../runtime-api.js";
3+
import type { StoredConversationReference } from "./conversation-store.js";
4+
import { resolveMSTeamsProactiveReplyStyle } from "./send-context.js";
5+
6+
function channelRef(params?: Partial<StoredConversationReference>): StoredConversationReference {
7+
return {
8+
user: { id: "user-1" },
9+
agent: { id: "agent-1" },
10+
conversation: { id: "19:[email protected]", conversationType: "channel" },
11+
channelId: "msteams",
12+
teamId: "team-1",
13+
...params,
14+
};
15+
}
16+
17+
describe("resolveMSTeamsProactiveReplyStyle", () => {
18+
it("uses thread for channel conversations with a stored thread root", () => {
19+
expect(
20+
resolveMSTeamsProactiveReplyStyle({
21+
cfg: {},
22+
conversationId: "19:[email protected]",
23+
ref: channelRef({ threadId: "thread-root-1" }),
24+
conversationType: "channel",
25+
}),
26+
).toBe("thread");
27+
});
28+
29+
it("falls back to activityId for legacy channel references", () => {
30+
expect(
31+
resolveMSTeamsProactiveReplyStyle({
32+
cfg: {},
33+
conversationId: "19:[email protected]",
34+
ref: channelRef({ activityId: "legacy-root-1" }),
35+
conversationType: "channel",
36+
}),
37+
).toBe("thread");
38+
});
39+
40+
it("keeps configured top-level channel routing", () => {
41+
const cfg: MSTeamsConfig = {
42+
replyStyle: "thread",
43+
teams: {
44+
"team-1": {
45+
channels: {
46+
"19:[email protected]": { replyStyle: "top-level" },
47+
},
48+
},
49+
},
50+
};
51+
52+
expect(
53+
resolveMSTeamsProactiveReplyStyle({
54+
cfg,
55+
conversationId: "19:[email protected]",
56+
ref: channelRef({ threadId: "thread-root-1" }),
57+
conversationType: "channel",
58+
}),
59+
).toBe("top-level");
60+
});
61+
62+
it("uses top-level when a channel has no stored thread root", () => {
63+
expect(
64+
resolveMSTeamsProactiveReplyStyle({
65+
cfg: { replyStyle: "thread" },
66+
conversationId: "19:[email protected]",
67+
ref: channelRef(),
68+
conversationType: "channel",
69+
}),
70+
).toBe("top-level");
71+
});
72+
73+
it("uses top-level for non-channel conversations", () => {
74+
const ref = channelRef({ activityId: "activity-1" });
75+
76+
expect(
77+
resolveMSTeamsProactiveReplyStyle({
78+
cfg: { replyStyle: "thread" },
79+
conversationId: "19:[email protected]",
80+
ref,
81+
conversationType: "groupChat",
82+
}),
83+
).toBe("top-level");
84+
expect(
85+
resolveMSTeamsProactiveReplyStyle({
86+
cfg: { replyStyle: "thread" },
87+
conversationId: "a:personal",
88+
ref,
89+
conversationType: "personal",
90+
}),
91+
).toBe("top-level");
92+
});
93+
});

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
22
import {
33
resolveChannelMediaMaxBytes,
4+
type MSTeamsConfig,
5+
type MSTeamsReplyStyle,
46
type OpenClawConfig,
57
type PluginRuntime,
68
} from "../runtime-api.js";
@@ -13,6 +15,7 @@ import type {
1315
import { formatUnknownError } from "./errors.js";
1416
import { resolveGraphChatId } from "./graph-upload.js";
1517
import type { MSTeamsAdapter } from "./messenger.js";
18+
import { resolveMSTeamsReplyPolicy, resolveMSTeamsRouteConfig } from "./policy.js";
1619
import { getMSTeamsRuntime } from "./runtime.js";
1720
import { createMSTeamsAdapter, createMSTeamsTokenProvider, loadMSTeamsSdkWithAuth } from "./sdk.js";
1821
import { resolveMSTeamsCredentials } from "./token.js";
@@ -27,6 +30,8 @@ export type MSTeamsProactiveContext = {
2730
log: ReturnType<PluginRuntime["logging"]["getChildLogger"]>;
2831
/** The type of conversation: personal (1:1), groupChat, or channel */
2932
conversationType: MSTeamsConversationType;
33+
/** Reply style resolved for proactive text/media sends. */
34+
replyStyle: MSTeamsReplyStyle;
3035
/** Token provider for Graph API / OneDrive operations */
3136
tokenProvider: MSTeamsAccessTokenProvider;
3237
/** SharePoint site ID for file uploads in group chats/channels */
@@ -42,6 +47,32 @@ export type MSTeamsProactiveContext = {
4247
graphChatId?: string | null;
4348
};
4449

50+
export function resolveMSTeamsProactiveReplyStyle(params: {
51+
cfg?: MSTeamsConfig;
52+
conversationId: string;
53+
ref: StoredConversationReference;
54+
conversationType: MSTeamsConversationType;
55+
}): MSTeamsReplyStyle {
56+
const threadRootId = params.ref.threadId ?? params.ref.activityId;
57+
if (params.conversationType !== "channel" || !threadRootId) {
58+
return "top-level";
59+
}
60+
61+
const routeConfig = resolveMSTeamsRouteConfig({
62+
cfg: params.cfg,
63+
teamId: params.ref.teamId,
64+
conversationId: params.conversationId,
65+
allowNameMatching: false,
66+
});
67+
const { replyStyle } = resolveMSTeamsReplyPolicy({
68+
isDirectMessage: false,
69+
globalConfig: params.cfg,
70+
teamConfig: routeConfig.teamConfig,
71+
channelConfig: routeConfig.channelConfig,
72+
});
73+
return replyStyle;
74+
}
75+
4576
/**
4677
* Parse the target value into a conversation reference lookup key.
4778
* Supported formats:
@@ -167,6 +198,12 @@ export async function resolveMSTeamsSendContext(params: {
167198
// groupChat, or unknown defaults to groupChat behavior
168199
conversationType = "groupChat";
169200
}
201+
const replyStyle = resolveMSTeamsProactiveReplyStyle({
202+
cfg: msteamsCfg,
203+
conversationId,
204+
ref,
205+
conversationType,
206+
});
170207

171208
// Get SharePoint site ID from config (required for file uploads in group chats/channels)
172209
const sharePointSiteId = msteamsCfg.sharePointSiteId;
@@ -223,6 +260,7 @@ export async function resolveMSTeamsSendContext(params: {
223260
adapter: adapter as unknown as MSTeamsAdapter,
224261
log,
225262
conversationType,
263+
replyStyle,
226264
tokenProvider,
227265
sharePointSiteId,
228266
mediaMaxBytes,

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ function createSharePointSendContext(params: {
116116
ref: {},
117117
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
118118
conversationType: "groupChat" as const,
119+
replyStyle: "top-level" as const,
119120
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
120121
mediaMaxBytes: 8 * 1024 * 1024,
121122
sharePointSiteId: params.siteId,
@@ -184,6 +185,7 @@ describe("sendMessageMSTeams", () => {
184185
ref: {},
185186
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
186187
conversationType: "personal",
188+
replyStyle: "top-level",
187189
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
188190
mediaMaxBytes: 8 * 1024,
189191
sharePointSiteId: undefined,
@@ -266,6 +268,62 @@ describe("sendMessageMSTeams", () => {
266268
expect(mockState.convertMarkdownTables).toHaveBeenCalledWith("hello", "off");
267269
});
268270

271+
it("passes the resolved proactive replyStyle to text sends", async () => {
272+
mockState.resolveMSTeamsSendContext.mockResolvedValue({
273+
adapter: {},
274+
appId: "app-id",
275+
conversationId: "19:[email protected]",
276+
ref: {
277+
threadId: "thread-root-1",
278+
conversation: { id: "19:[email protected]", conversationType: "channel" },
279+
},
280+
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
281+
conversationType: "channel",
282+
replyStyle: "thread",
283+
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
284+
mediaMaxBytes: 8 * 1024,
285+
sharePointSiteId: undefined,
286+
});
287+
288+
await sendMessageMSTeams({
289+
cfg: {} as OpenClawConfig,
290+
to: "conversation:19:[email protected]",
291+
text: "threaded reply",
292+
});
293+
294+
expect(mockState.sendMSTeamsMessages).toHaveBeenCalledWith(
295+
expect.objectContaining({ replyStyle: "thread" }),
296+
);
297+
});
298+
299+
it("keeps top-level proactive replyStyle when resolved for a channel", async () => {
300+
mockState.resolveMSTeamsSendContext.mockResolvedValue({
301+
adapter: {},
302+
appId: "app-id",
303+
conversationId: "19:[email protected]",
304+
ref: {
305+
threadId: "thread-root-1",
306+
conversation: { id: "19:[email protected]", conversationType: "channel" },
307+
},
308+
log: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
309+
conversationType: "channel",
310+
replyStyle: "top-level",
311+
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
312+
mediaMaxBytes: 8 * 1024,
313+
sharePointSiteId: undefined,
314+
});
315+
316+
await sendMessageMSTeams({
317+
cfg: {} as OpenClawConfig,
318+
to: "conversation:19:[email protected]",
319+
text: "top-level reply",
320+
});
321+
322+
expect(mockState.sendMSTeamsMessages).toHaveBeenCalledWith(
323+
expect.objectContaining({ replyStyle: "top-level" }),
324+
);
325+
});
326+
269327
it("uses graphChatId instead of conversationId when uploading to SharePoint", async () => {
270328
// Simulates a group chat where Bot Framework conversationId is valid but we have
271329
// a resolved Graph chat ID cached from a prior send.

‎extensions/msteams/src/send.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,13 @@ async function sendTextWithMedia(
391391
tokenProvider,
392392
sharePointSiteId,
393393
mediaMaxBytes,
394+
replyStyle,
394395
} = ctx;
395396

396397
let platformMessageIds: string[];
397398
try {
398399
platformMessageIds = await sendMSTeamsMessages({
399-
replyStyle: "top-level",
400+
replyStyle,
400401
adapter,
401402
appId,
402403
conversationRef: ref,

0 commit comments

Comments
 (0)