Skip to content

Commit 5cb4bbc

Browse files
authored
fix(msteams): use one Teams API runtime entrypoint (#128572)
* fix(msteams): use one Teams API runtime entrypoint * test(msteams): exercise packaged CJS loader * test(msteams): run packaged build through CLI * chore(msteams): document SDK assertion safety
1 parent 628b7a2 commit 5cb4bbc

2 files changed

Lines changed: 175 additions & 10 deletions

File tree

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Microsoft Teams process coverage protects real SDK CommonJS import ordering.
2+
import { execFile } from "node:child_process";
3+
import fs from "node:fs";
4+
import path from "node:path";
5+
import { promisify } from "node:util";
6+
import { describe, expect, it } from "vitest";
7+
8+
const execFileAsync = promisify(execFile);
9+
10+
describe("sendMSTeamsActivityWithReference SDK import ordering", () => {
11+
it("keeps root exports intact when quoted behavior is the first SDK access", async () => {
12+
await execFileAsync(
13+
process.execPath,
14+
["scripts/lib/plugin-npm-runtime-build.mjs", "extensions/msteams"],
15+
{ cwd: process.cwd() },
16+
);
17+
const outDir = path.join(process.cwd(), "extensions/msteams/dist");
18+
const proactiveArtifact = fs
19+
.readdirSync(outDir)
20+
.filter((entry) => entry.endsWith(".cjs"))
21+
.map((entry) => path.join(outDir, entry))
22+
.find((entry) =>
23+
fs
24+
.readFileSync(entry, "utf8")
25+
.includes('Object.defineProperty(exports, "sendMSTeamsActivityWithReference"'),
26+
);
27+
if (!proactiveArtifact) {
28+
throw new Error("Microsoft Teams CommonJS runtime omitted the proactive send helper");
29+
}
30+
const proactiveArtifactSource = fs.readFileSync(proactiveArtifact, "utf8");
31+
expect(proactiveArtifactSource).toContain('import("@microsoft/teams.api")');
32+
expect(proactiveArtifactSource).not.toContain("@microsoft/teams.api/dist/");
33+
34+
const fixture = `
35+
import assert from "node:assert/strict";
36+
import { createRequire } from "node:module";
37+
38+
const require = createRequire(import.meta.url);
39+
const Module = require("node:module");
40+
const originalLoad = Module._load;
41+
const universalStub = new Proxy(
42+
function universalStub() {
43+
return universalStub;
44+
},
45+
{
46+
apply: () => universalStub,
47+
construct: () => universalStub,
48+
get: () => universalStub,
49+
},
50+
);
51+
const hostSdkStub = new Proxy(
52+
{},
53+
{
54+
get: (_target, key) => {
55+
if (key === "createLazyRuntimeModule") {
56+
return (importer) => {
57+
let pending;
58+
return () => (pending ??= importer());
59+
};
60+
}
61+
if (key === "getOrCreateGlobalSingleton") {
62+
return (_key, create) => create();
63+
}
64+
return universalStub;
65+
},
66+
},
67+
);
68+
// The built plugin expects an installed OpenClaw host. Stub unrelated host SDK exports so
69+
// this child isolates the emitted Teams loader and the real pinned Teams CommonJS package.
70+
Module._load = function load(request, parent, isMain) {
71+
if (request.startsWith("openclaw/plugin-sdk/")) {
72+
return hostSdkStub;
73+
}
74+
return originalLoad.call(this, request, parent, isMain);
75+
};
76+
77+
const { sendMSTeamsActivityWithReference } =
78+
require(process.env.OPENCLAW_MSTEAMS_PROACTIVE_ARTIFACT);
79+
const quotedCreates = [];
80+
const posts = [];
81+
const app = {
82+
client: {
83+
request: async () => ({}),
84+
post: async (url, activity) => {
85+
posts.push({ url, activity });
86+
return { data: { id: "normal-proactive" } };
87+
},
88+
},
89+
api: {
90+
serviceUrl: "https://smba.trafficmanager.net/amer",
91+
conversations: {
92+
activities: () => ({
93+
create: async (activity) => {
94+
quotedCreates.push(activity);
95+
return { id: "quoted-replay" };
96+
},
97+
update: async () => ({}),
98+
delete: async () => ({}),
99+
}),
100+
},
101+
},
102+
};
103+
const reference = {
104+
agent: { id: "28:bot", role: "bot" },
105+
user: { id: "29:user" },
106+
conversation: {
107+
108+
conversationType: "groupChat",
109+
},
110+
};
111+
112+
// The structural app selects the quote path without loading the full Teams app first.
113+
await sendMSTeamsActivityWithReference(
114+
app,
115+
{ ...reference, serviceUrl: "https://smba.trafficmanager.net/amer" },
116+
{ type: "message", text: "Recovered reply" },
117+
{ quoteActivityId: "incoming-activity" },
118+
);
119+
assert.equal(
120+
quotedCreates[0].text,
121+
'<quoted messageId="incoming-activity"/> Recovered reply',
122+
);
123+
124+
const api = await import("@microsoft/teams.api");
125+
assert.equal(typeof api.Client, "function");
126+
assert.equal(typeof api.MessageActivityInput, "function");
127+
assert.equal(typeof api.toActivityParams, "function");
128+
assert.doesNotThrow(() =>
129+
api.toActivityParams({ type: "message", text: "Direct conversion" }),
130+
);
131+
132+
await assert.doesNotReject(() =>
133+
sendMSTeamsActivityWithReference(
134+
app,
135+
{ ...reference, serviceUrl: "https://smba.trafficmanager.net/teams" },
136+
{ type: "message", text: "Normal send" },
137+
),
138+
);
139+
assert.equal(posts.length, 1);
140+
assert.equal(
141+
posts[0].url,
142+
"https://smba.trafficmanager.net/teams/v3/conversations/19:[email protected]/activities",
143+
);
144+
assert.equal(posts[0].activity.text, "Normal send");
145+
process.stdout.write("root-sdk-ok");
146+
`;
147+
148+
const { stdout, stderr } = await execFileAsync(
149+
process.execPath,
150+
["--import", "tsx", "--input-type=module", "--eval", fixture],
151+
{
152+
cwd: process.cwd(),
153+
env: {
154+
...process.env,
155+
NODE_DISABLE_COMPILE_CACHE: "1",
156+
OPENCLAW_MSTEAMS_PROACTIVE_ARTIFACT: proactiveArtifact,
157+
VITEST: undefined,
158+
},
159+
},
160+
);
161+
162+
expect(stderr).toBe("");
163+
expect(stdout).toBe("root-sdk-ok");
164+
}, 30_000);
165+
});

‎extensions/msteams/src/sdk-proactive.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ type MSTeamsProactiveOptions = {
6464
serviceUrlBoundary?: MSTeamsSdkCloudOptions;
6565
};
6666

67-
const loadMSTeamsApiClient = createLazyRuntimeModule(() =>
68-
import("@microsoft/teams.api").then((api) => ({ Client: api.Client })),
69-
);
70-
71-
const loadMSTeamsQuoteModule = createLazyRuntimeModule(() =>
72-
import("@microsoft/teams.api/dist/activities/message/message.js").then((message) => ({
73-
MessageActivityInput: message.MessageActivityInput,
74-
})),
67+
const loadMSTeamsApiModule = createLazyRuntimeModule(
68+
() =>
69+
// SAFETY: the 2.0.14 CJS root exports MessageActivityInput, while its declarations omit it.
70+
import("@microsoft/teams.api") as unknown as Promise<
71+
typeof import("@microsoft/teams.api") & {
72+
MessageActivityInput: typeof import("@microsoft/teams.api/dist/activities/message/message.js").MessageActivityInput;
73+
}
74+
>,
7575
);
7676

7777
async function quoteMSTeamsActivity(
7878
activity: MSTeamsActivityLike,
7979
messageId: string,
8080
): Promise<unknown> {
81-
const { MessageActivityInput } = await loadMSTeamsQuoteModule();
81+
const { MessageActivityInput } = await loadMSTeamsApiModule();
8282
if (typeof activity === "string") {
8383
return new MessageActivityInput(activity).prependQuote(messageId);
8484
}
@@ -203,7 +203,7 @@ async function getApiClientForReference(
203203
return api;
204204
}
205205

206-
const { Client } = await loadMSTeamsApiClient();
206+
const { Client } = await loadMSTeamsApiModule();
207207
return new Client(ref.serviceUrl, httpClient) as unknown as MSTeamsApiClient;
208208
}
209209

0 commit comments

Comments
 (0)