@kodelyth/msteams 2026.5.39 → 2026.5.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/api.ts +3 -0
  2. package/channel-config-api.ts +1 -0
  3. package/channel-plugin-api.ts +2 -0
  4. package/config-api.ts +4 -0
  5. package/contract-api.ts +4 -0
  6. package/dist/api.js +3 -0
  7. package/dist/channel-BvTXHuGs.js +1161 -0
  8. package/dist/channel-config-api.js +2 -0
  9. package/dist/channel-plugin-api.js +2 -0
  10. package/dist/channel.runtime-NssGKZm5.js +650 -0
  11. package/dist/config-schema-Btk-XCOd.js +43 -0
  12. package/dist/contract-api.js +2 -0
  13. package/dist/graph-users-D-gKCguI.js +1411 -0
  14. package/dist/index.js +22 -0
  15. package/dist/oauth-BUxlphX3.js +114 -0
  16. package/dist/oauth.token-ebId9946.js +116 -0
  17. package/dist/probe-Cj2KsAGF.js +2190 -0
  18. package/dist/runtime-api-BL4DOWXD.js +28 -0
  19. package/dist/runtime-api.js +2 -0
  20. package/dist/secret-contract-Bo7kdUrT.js +35 -0
  21. package/dist/secret-contract-api.js +2 -0
  22. package/dist/setup-entry.js +15 -0
  23. package/dist/setup-plugin-api.js +64 -0
  24. package/dist/setup-surface-COTQDcTQ.js +531 -0
  25. package/dist/src-tvpsGYPV.js +4226 -0
  26. package/dist/test-api.js +2 -0
  27. package/index.ts +20 -0
  28. package/klaw.plugin.json +2 -726
  29. package/package.json +4 -4
  30. package/runtime-api.ts +66 -0
  31. package/secret-contract-api.ts +5 -0
  32. package/setup-entry.ts +13 -0
  33. package/setup-plugin-api.ts +3 -0
  34. package/src/ai-entity.ts +7 -0
  35. package/src/approval-auth.ts +44 -0
  36. package/src/attachments/bot-framework.test.ts +506 -0
  37. package/src/attachments/bot-framework.ts +348 -0
  38. package/src/attachments/download.ts +328 -0
  39. package/src/attachments/graph.test.ts +441 -0
  40. package/src/attachments/graph.ts +489 -0
  41. package/src/attachments/html.ts +122 -0
  42. package/src/attachments/payload.ts +14 -0
  43. package/src/attachments/remote-media.test.ts +187 -0
  44. package/src/attachments/remote-media.ts +86 -0
  45. package/src/attachments/shared.test.ts +547 -0
  46. package/src/attachments/shared.ts +655 -0
  47. package/src/attachments/types.ts +47 -0
  48. package/src/attachments.graph.test.ts +414 -0
  49. package/src/attachments.helpers.test.ts +245 -0
  50. package/src/attachments.test-helpers.ts +17 -0
  51. package/src/attachments.test.ts +754 -0
  52. package/src/attachments.ts +18 -0
  53. package/src/block-streaming-config.test.ts +61 -0
  54. package/src/channel-api.ts +1 -0
  55. package/src/channel.actions.test.ts +797 -0
  56. package/src/channel.directory.test.ts +176 -0
  57. package/src/channel.message-adapter.test.ts +227 -0
  58. package/src/channel.runtime.ts +56 -0
  59. package/src/channel.setup.ts +77 -0
  60. package/src/channel.test.ts +136 -0
  61. package/src/channel.ts +1176 -0
  62. package/src/config-schema.ts +6 -0
  63. package/src/config-ui-hints.ts +40 -0
  64. package/src/conversation-store-fs.test.ts +81 -0
  65. package/src/conversation-store-fs.ts +149 -0
  66. package/src/conversation-store-helpers.test.ts +202 -0
  67. package/src/conversation-store-helpers.ts +105 -0
  68. package/src/conversation-store-memory.ts +51 -0
  69. package/src/conversation-store.shared.test.ts +260 -0
  70. package/src/conversation-store.ts +71 -0
  71. package/src/directory-live.test.ts +156 -0
  72. package/src/directory-live.ts +111 -0
  73. package/src/doctor.ts +27 -0
  74. package/src/errors.test.ts +154 -0
  75. package/src/errors.ts +270 -0
  76. package/src/feedback-reflection-prompt.ts +117 -0
  77. package/src/feedback-reflection-store.ts +113 -0
  78. package/src/feedback-reflection.test.ts +237 -0
  79. package/src/feedback-reflection.ts +268 -0
  80. package/src/file-consent-helpers.test.ts +328 -0
  81. package/src/file-consent-helpers.ts +115 -0
  82. package/src/file-consent-invoke.ts +150 -0
  83. package/src/file-consent.test.ts +378 -0
  84. package/src/file-consent.ts +223 -0
  85. package/src/graph-chat.ts +36 -0
  86. package/src/graph-group-management.test.ts +332 -0
  87. package/src/graph-group-management.ts +168 -0
  88. package/src/graph-members.test.ts +89 -0
  89. package/src/graph-members.ts +48 -0
  90. package/src/graph-messages.actions.test.ts +253 -0
  91. package/src/graph-messages.read.test.ts +391 -0
  92. package/src/graph-messages.search.test.ts +227 -0
  93. package/src/graph-messages.test-helpers.ts +50 -0
  94. package/src/graph-messages.ts +534 -0
  95. package/src/graph-teams.test.ts +222 -0
  96. package/src/graph-teams.ts +114 -0
  97. package/src/graph-thread.test.ts +252 -0
  98. package/src/graph-thread.ts +146 -0
  99. package/src/graph-upload.test.ts +253 -0
  100. package/src/graph-upload.ts +531 -0
  101. package/src/graph-users.ts +29 -0
  102. package/src/graph.test.ts +540 -0
  103. package/src/graph.ts +308 -0
  104. package/src/inbound.test.ts +221 -0
  105. package/src/inbound.ts +148 -0
  106. package/src/index.ts +4 -0
  107. package/src/media-helpers.test.ts +220 -0
  108. package/src/media-helpers.ts +105 -0
  109. package/src/mentions.test.ts +254 -0
  110. package/src/mentions.ts +114 -0
  111. package/src/messenger.test.ts +961 -0
  112. package/src/messenger.ts +608 -0
  113. package/src/monitor-handler/access.ts +136 -0
  114. package/src/monitor-handler/inbound-media.test.ts +314 -0
  115. package/src/monitor-handler/inbound-media.ts +180 -0
  116. package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
  117. package/src/monitor-handler/message-handler.authz.test.ts +739 -0
  118. package/src/monitor-handler/message-handler.dm-media.test.ts +54 -0
  119. package/src/monitor-handler/message-handler.test-support.ts +99 -0
  120. package/src/monitor-handler/message-handler.thread-parent.test.ts +225 -0
  121. package/src/monitor-handler/message-handler.thread-session.test.ts +132 -0
  122. package/src/monitor-handler/message-handler.ts +1003 -0
  123. package/src/monitor-handler/reaction-handler.test.ts +325 -0
  124. package/src/monitor-handler/reaction-handler.ts +122 -0
  125. package/src/monitor-handler/thread-session.ts +30 -0
  126. package/src/monitor-handler.adaptive-card.test.ts +158 -0
  127. package/src/monitor-handler.feedback-authz.test.ts +357 -0
  128. package/src/monitor-handler.file-consent.test.ts +443 -0
  129. package/src/monitor-handler.sso.test.ts +576 -0
  130. package/src/monitor-handler.test-helpers.ts +181 -0
  131. package/src/monitor-handler.ts +538 -0
  132. package/src/monitor-handler.types.ts +27 -0
  133. package/src/monitor-types.ts +6 -0
  134. package/src/monitor.lifecycle.test.ts +457 -0
  135. package/src/monitor.test.ts +119 -0
  136. package/src/monitor.ts +476 -0
  137. package/src/oauth.flow.ts +77 -0
  138. package/src/oauth.shared.ts +37 -0
  139. package/src/oauth.test.ts +350 -0
  140. package/src/oauth.token.ts +162 -0
  141. package/src/oauth.ts +130 -0
  142. package/src/outbound.test.ts +400 -0
  143. package/src/outbound.ts +198 -0
  144. package/src/pending-uploads-fs.test.ts +261 -0
  145. package/src/pending-uploads-fs.ts +235 -0
  146. package/src/pending-uploads.test.ts +186 -0
  147. package/src/pending-uploads.ts +121 -0
  148. package/src/policy.test.ts +156 -0
  149. package/src/policy.ts +245 -0
  150. package/src/polls-store-memory.ts +32 -0
  151. package/src/polls.test.ts +169 -0
  152. package/src/polls.ts +312 -0
  153. package/src/presentation.ts +93 -0
  154. package/src/probe.test.ts +79 -0
  155. package/src/probe.ts +132 -0
  156. package/src/reply-dispatcher.test.ts +543 -0
  157. package/src/reply-dispatcher.ts +523 -0
  158. package/src/reply-stream-controller.test.ts +424 -0
  159. package/src/reply-stream-controller.ts +334 -0
  160. package/src/resolve-allowlist.test.ts +253 -0
  161. package/src/resolve-allowlist.ts +309 -0
  162. package/src/revoked-context.ts +17 -0
  163. package/src/runtime.ts +12 -0
  164. package/src/sdk-types.ts +59 -0
  165. package/src/sdk.test.ts +727 -0
  166. package/src/sdk.ts +916 -0
  167. package/src/secret-contract.ts +49 -0
  168. package/src/secret-input.ts +7 -0
  169. package/src/send-context.test.ts +93 -0
  170. package/src/send-context.ts +269 -0
  171. package/src/send.test.ts +588 -0
  172. package/src/send.ts +697 -0
  173. package/src/sent-message-cache.test.ts +106 -0
  174. package/src/sent-message-cache.ts +174 -0
  175. package/src/session-route.ts +40 -0
  176. package/src/setup-core.ts +162 -0
  177. package/src/setup-surface.test.ts +175 -0
  178. package/src/setup-surface.ts +319 -0
  179. package/src/sso-token-store.test.ts +74 -0
  180. package/src/sso-token-store.ts +166 -0
  181. package/src/sso.ts +300 -0
  182. package/src/storage.ts +25 -0
  183. package/src/store-fs.ts +42 -0
  184. package/src/streaming-message.test.ts +323 -0
  185. package/src/streaming-message.ts +327 -0
  186. package/src/test-runtime.ts +16 -0
  187. package/src/thread-parent-context.test.ts +224 -0
  188. package/src/thread-parent-context.ts +159 -0
  189. package/src/token-response.ts +11 -0
  190. package/src/token.test.ts +268 -0
  191. package/src/token.ts +194 -0
  192. package/src/user-agent.test.ts +121 -0
  193. package/src/user-agent.ts +53 -0
  194. package/src/webhook-timeouts.ts +27 -0
  195. package/src/welcome-card.test.ts +104 -0
  196. package/src/welcome-card.ts +57 -0
  197. package/test-api.ts +1 -0
  198. package/tsconfig.json +16 -0
  199. package/api.js +0 -7
  200. package/channel-config-api.js +0 -7
  201. package/channel-plugin-api.js +0 -7
  202. package/contract-api.js +0 -7
  203. package/index.js +0 -7
  204. package/runtime-api.js +0 -7
  205. package/secret-contract-api.js +0 -7
  206. package/setup-entry.js +0 -7
  207. package/setup-plugin-api.js +0 -7
  208. package/test-api.js +0 -7
package/src/graph.ts ADDED
@@ -0,0 +1,308 @@
1
+ import { readProviderJsonResponse } from "klaw/plugin-sdk/provider-http";
2
+ import { fetchWithSsrFGuard, type MSTeamsConfig } from "../runtime-api.js";
3
+ import { GRAPH_ROOT } from "./attachments/shared.js";
4
+
5
+ const GRAPH_BETA = "https://graph.microsoft.com/beta";
6
+ const NULL_BODY_STATUSES = new Set([101, 204, 205, 304]);
7
+ import { createMSTeamsTokenProvider, loadMSTeamsSdkWithAuth } from "./sdk.js";
8
+ import { readAccessToken } from "./token-response.js";
9
+ import { resolveDelegatedAccessToken, resolveMSTeamsCredentials } from "./token.js";
10
+ import { buildUserAgent } from "./user-agent.js";
11
+
12
+ export type GraphUser = {
13
+ id?: string;
14
+ displayName?: string;
15
+ userPrincipalName?: string;
16
+ mail?: string;
17
+ };
18
+
19
+ type GraphGroup = {
20
+ id?: string;
21
+ displayName?: string;
22
+ };
23
+
24
+ type GraphChannel = {
25
+ id?: string;
26
+ displayName?: string;
27
+ };
28
+
29
+ export type GraphResponse<T> = { value?: T[] };
30
+
31
+ export function normalizeQuery(value?: string | null): string {
32
+ return value?.trim() ?? "";
33
+ }
34
+
35
+ export function escapeOData(value: string): string {
36
+ return value.replace(/'/g, "''");
37
+ }
38
+
39
+ async function requestGraph(params: {
40
+ token: string;
41
+ path: string;
42
+ method?: "GET" | "POST" | "PATCH" | "DELETE";
43
+ root?: string;
44
+ headers?: Record<string, string>;
45
+ body?: unknown;
46
+ errorPrefix?: string;
47
+ }): Promise<Response> {
48
+ const hasBody = params.body !== undefined;
49
+ const url = `${params.root ?? GRAPH_ROOT}${params.path}`;
50
+ const currentFetch = globalThis.fetch;
51
+ const { response, release } = await fetchWithSsrFGuard({
52
+ url,
53
+ fetchImpl: async (input, guardedInit) => await currentFetch(input, guardedInit),
54
+ init: {
55
+ method: params.method,
56
+ headers: {
57
+ "User-Agent": buildUserAgent(),
58
+ Authorization: `Bearer ${params.token}`,
59
+ ...(hasBody ? { "Content-Type": "application/json" } : {}),
60
+ ...params.headers,
61
+ },
62
+ body: hasBody ? JSON.stringify(params.body) : undefined,
63
+ },
64
+ auditContext: "msteams.graph",
65
+ });
66
+ try {
67
+ if (!response.ok) {
68
+ const text = await response.text().catch(() => "");
69
+ throw new Error(
70
+ `${params.errorPrefix ?? "Graph"} ${params.path} failed (${response.status}): ${text || "unknown error"}`,
71
+ );
72
+ }
73
+ const body = NULL_BODY_STATUSES.has(response.status) ? null : await response.arrayBuffer();
74
+ return new Response(body, {
75
+ status: response.status,
76
+ statusText: response.statusText,
77
+ headers: new Headers(response.headers),
78
+ });
79
+ } finally {
80
+ await release();
81
+ }
82
+ }
83
+
84
+ async function readOptionalGraphJson<T>(res: Response, label: string): Promise<T> {
85
+ // Use optional chaining to stay resilient to partial test mocks that do not
86
+ // provide a status or Headers instance (they only shim `ok` + `json()`).
87
+ if (res.status === 204 || res.headers?.get?.("content-length") === "0") {
88
+ return undefined as T;
89
+ }
90
+ return await readProviderJsonResponse<T>(res, label);
91
+ }
92
+
93
+ export async function fetchGraphJson<T>(params: {
94
+ token: string;
95
+ path: string;
96
+ headers?: Record<string, string>;
97
+ /** HTTP method; defaults to "GET" */
98
+ method?: string;
99
+ /** Request body (serialized as JSON). Only used for non-GET methods. */
100
+ body?: unknown;
101
+ }): Promise<T> {
102
+ const res = await requestGraph({
103
+ token: params.token,
104
+ path: params.path,
105
+ method: params.method as "GET" | "POST" | "DELETE" | undefined,
106
+ body: params.body,
107
+ headers: params.headers,
108
+ });
109
+ return await readOptionalGraphJson<T>(res, `Graph ${params.path} failed`);
110
+ }
111
+
112
+ /**
113
+ * Fetch JSON from an absolute Graph API URL (for example @odata.nextLink
114
+ * pagination URLs) without prepending GRAPH_ROOT.
115
+ */
116
+ export async function fetchGraphAbsoluteUrl<T>(params: {
117
+ token: string;
118
+ url: string;
119
+ headers?: Record<string, string>;
120
+ }): Promise<T> {
121
+ const { response, release } = await fetchWithSsrFGuard({
122
+ url: params.url,
123
+ init: {
124
+ headers: {
125
+ "User-Agent": buildUserAgent(),
126
+ Authorization: `Bearer ${params.token}`,
127
+ ...params.headers,
128
+ },
129
+ },
130
+ auditContext: "msteams.graph.absolute",
131
+ });
132
+ try {
133
+ if (!response.ok) {
134
+ const text = await response.text().catch(() => "");
135
+ throw new Error(
136
+ `Graph ${params.url} failed (${response.status}): ${text || "unknown error"}`,
137
+ );
138
+ }
139
+ return await readProviderJsonResponse<T>(response, `Graph ${params.url} failed`);
140
+ } finally {
141
+ await release();
142
+ }
143
+ }
144
+
145
+ /** Graph collection response with optional pagination link. */
146
+ type GraphPagedResponse<T> = {
147
+ value?: T[];
148
+ "@odata.nextLink"?: string;
149
+ };
150
+
151
+ /** Result of a paginated Graph API fetch. */
152
+ type PaginatedResult<T> = {
153
+ items: T[];
154
+ truncated: boolean;
155
+ found?: T;
156
+ };
157
+
158
+ /**
159
+ * Fetch all pages of a Graph API collection, following @odata.nextLink.
160
+ * Optionally stop early when `findOne` matches an item.
161
+ */
162
+ export async function fetchAllGraphPages<T>(params: {
163
+ token: string;
164
+ path: string;
165
+ headers?: Record<string, string>;
166
+ /** Max pages to fetch before stopping. Default: 50. */
167
+ maxPages?: number;
168
+ /** Stop pagination early when this predicate returns true. */
169
+ findOne?: (item: T) => boolean;
170
+ }): Promise<PaginatedResult<T>> {
171
+ const maxPages = params.maxPages ?? 50;
172
+ const items: T[] = [];
173
+ let nextPath: string | undefined = params.path;
174
+
175
+ for (let page = 0; page < maxPages && nextPath; page++) {
176
+ const res: GraphPagedResponse<T> = await fetchGraphJson<GraphPagedResponse<T>>({
177
+ token: params.token,
178
+ path: nextPath,
179
+ headers: params.headers,
180
+ });
181
+
182
+ const pageItems = res.value ?? [];
183
+
184
+ if (params.findOne) {
185
+ const match = pageItems.find(params.findOne);
186
+ if (match) {
187
+ items.push(...pageItems);
188
+ return { items, truncated: false, found: match };
189
+ }
190
+ }
191
+
192
+ items.push(...pageItems);
193
+
194
+ // @odata.nextLink is an absolute URL; strip the Graph root to get a relative path
195
+ const rawNext: string | undefined = res["@odata.nextLink"];
196
+ if (rawNext) {
197
+ nextPath = rawNext
198
+ .replace("https://graph.microsoft.com/v1.0", "")
199
+ .replace("https://graph.microsoft.com/beta", "");
200
+ } else {
201
+ nextPath = undefined;
202
+ }
203
+ }
204
+
205
+ return { items, truncated: Boolean(nextPath) };
206
+ }
207
+
208
+ export async function resolveGraphToken(
209
+ cfg: unknown,
210
+ options?: { preferDelegated?: boolean },
211
+ ): Promise<string> {
212
+ const msteamsCfg = (cfg as { channels?: { msteams?: MSTeamsConfig } })?.channels?.msteams;
213
+ const creds = resolveMSTeamsCredentials(msteamsCfg);
214
+ if (!creds) {
215
+ throw new Error("MS Teams credentials missing");
216
+ }
217
+
218
+ // Try delegated token if requested and configured
219
+ if (options?.preferDelegated && msteamsCfg?.delegatedAuth?.enabled && creds.type === "secret") {
220
+ const delegated = await resolveDelegatedAccessToken({
221
+ tenantId: creds.tenantId,
222
+ clientId: creds.appId,
223
+ clientSecret: creds.appPassword,
224
+ });
225
+ if (delegated) {
226
+ return delegated;
227
+ }
228
+ // Fall through to app-only token
229
+ }
230
+
231
+ const { app } = await loadMSTeamsSdkWithAuth(creds);
232
+ const tokenProvider = createMSTeamsTokenProvider(app);
233
+ const graphTokenValue = await tokenProvider.getAccessToken("https://graph.microsoft.com");
234
+ const accessToken = readAccessToken(graphTokenValue);
235
+ if (!accessToken) {
236
+ throw new Error("MS Teams graph token unavailable");
237
+ }
238
+ return accessToken;
239
+ }
240
+
241
+ export async function listTeamsByName(token: string, query: string): Promise<GraphGroup[]> {
242
+ const escaped = escapeOData(query);
243
+ const filter = `resourceProvisioningOptions/Any(x:x eq 'Team') and startsWith(displayName,'${escaped}')`;
244
+ const path = `/groups?$filter=${encodeURIComponent(filter)}&$select=id,displayName`;
245
+ const { items } = await fetchAllGraphPages<GraphGroup>({ token, path, maxPages: 5 });
246
+ return items;
247
+ }
248
+
249
+ export async function postGraphJson<T>(params: {
250
+ token: string;
251
+ path: string;
252
+ body?: unknown;
253
+ }): Promise<T> {
254
+ const res = await requestGraph({
255
+ token: params.token,
256
+ path: params.path,
257
+ method: "POST",
258
+ body: params.body,
259
+ errorPrefix: "Graph POST",
260
+ });
261
+ return readOptionalGraphJson<T>(res, `Graph POST ${params.path} failed`);
262
+ }
263
+
264
+ export async function postGraphBetaJson<T>(params: {
265
+ token: string;
266
+ path: string;
267
+ body?: unknown;
268
+ }): Promise<T> {
269
+ const res = await requestGraph({
270
+ token: params.token,
271
+ path: params.path,
272
+ method: "POST",
273
+ root: GRAPH_BETA,
274
+ body: params.body,
275
+ errorPrefix: "Graph beta POST",
276
+ });
277
+ return readOptionalGraphJson<T>(res, `Graph beta POST ${params.path} failed`);
278
+ }
279
+
280
+ export async function deleteGraphRequest(params: { token: string; path: string }): Promise<void> {
281
+ await requestGraph({
282
+ token: params.token,
283
+ path: params.path,
284
+ method: "DELETE",
285
+ errorPrefix: "Graph DELETE",
286
+ });
287
+ }
288
+
289
+ export async function patchGraphJson<T>(params: {
290
+ token: string;
291
+ path: string;
292
+ body?: unknown;
293
+ }): Promise<T> {
294
+ const res = await requestGraph({
295
+ token: params.token,
296
+ path: params.path,
297
+ method: "PATCH",
298
+ body: params.body,
299
+ errorPrefix: "Graph PATCH",
300
+ });
301
+ return readOptionalGraphJson<T>(res, `Graph PATCH ${params.path} failed`);
302
+ }
303
+
304
+ export async function listChannelsForTeam(token: string, teamId: string): Promise<GraphChannel[]> {
305
+ const path = `/teams/${encodeURIComponent(teamId)}/channels?$select=id,displayName`;
306
+ const { items } = await fetchAllGraphPages<GraphChannel>({ token, path, maxPages: 10 });
307
+ return items;
308
+ }
@@ -0,0 +1,221 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ decodeHtmlEntities,
4
+ extractMSTeamsQuoteInfo,
5
+ htmlToPlainText,
6
+ normalizeMSTeamsConversationId,
7
+ parseMSTeamsActivityTimestamp,
8
+ stripMSTeamsMentionTags,
9
+ wasMSTeamsBotMentioned,
10
+ } from "./inbound.js";
11
+
12
+ describe("msteams inbound", () => {
13
+ describe("stripMSTeamsMentionTags", () => {
14
+ it("removes <at>...</at> tags and trims", () => {
15
+ expect(stripMSTeamsMentionTags("<at>Bot</at> hi")).toBe("hi");
16
+ expect(stripMSTeamsMentionTags("hi <at>Bot</at>")).toBe("hi");
17
+ });
18
+
19
+ it("removes <at ...> tags with attributes", () => {
20
+ expect(stripMSTeamsMentionTags('<at id="1">Bot</at> hi')).toBe("hi");
21
+ expect(stripMSTeamsMentionTags('hi <at itemid="2">Bot</at>')).toBe("hi");
22
+ });
23
+ });
24
+
25
+ describe("normalizeMSTeamsConversationId", () => {
26
+ it("strips the ;messageid suffix", () => {
27
+ expect(normalizeMSTeamsConversationId("19:abc@thread.tacv2;messageid=deadbeef")).toBe(
28
+ "19:abc@thread.tacv2",
29
+ );
30
+ });
31
+ });
32
+
33
+ describe("parseMSTeamsActivityTimestamp", () => {
34
+ it("returns undefined for empty/invalid values", () => {
35
+ expect(parseMSTeamsActivityTimestamp(undefined)).toBeUndefined();
36
+ expect(parseMSTeamsActivityTimestamp("not-a-date")).toBeUndefined();
37
+ });
38
+
39
+ it("parses string timestamps", () => {
40
+ const ts = parseMSTeamsActivityTimestamp("2024-01-01T00:00:00.000Z");
41
+ if (!ts) {
42
+ throw new Error("expected MSTeams timestamp parser to return a Date");
43
+ }
44
+ expect(ts.toISOString()).toBe("2024-01-01T00:00:00.000Z");
45
+ });
46
+
47
+ it("passes through Date instances", () => {
48
+ const d = new Date("2024-01-01T00:00:00.000Z");
49
+ expect(parseMSTeamsActivityTimestamp(d)).toBe(d);
50
+ });
51
+ });
52
+
53
+ describe("wasMSTeamsBotMentioned", () => {
54
+ it("returns true when a mention entity matches recipient.id", () => {
55
+ expect(
56
+ wasMSTeamsBotMentioned({
57
+ recipient: { id: "bot" },
58
+ entities: [{ type: "mention", mentioned: { id: "bot" } }],
59
+ }),
60
+ ).toBe(true);
61
+ });
62
+
63
+ it("returns false when there is no matching mention", () => {
64
+ expect(
65
+ wasMSTeamsBotMentioned({
66
+ recipient: { id: "bot" },
67
+ entities: [{ type: "mention", mentioned: { id: "other" } }],
68
+ }),
69
+ ).toBe(false);
70
+ });
71
+ });
72
+
73
+ describe("decodeHtmlEntities", () => {
74
+ it("decodes common entities", () => {
75
+ expect(decodeHtmlEntities("&amp;&lt;&gt;&quot;&#39;&#x27;&nbsp;")).toBe("&<>\"'' ");
76
+ });
77
+
78
+ it("leaves plain text unchanged", () => {
79
+ expect(decodeHtmlEntities("hello world")).toBe("hello world");
80
+ });
81
+
82
+ it("prevents double-decoding: &amp;lt; should become &lt; not <", () => {
83
+ // If &amp; were decoded first, &amp;lt; → &lt; → < (wrong).
84
+ // With &amp; decoded last, &amp;lt; stays as &lt; (correct).
85
+ expect(decodeHtmlEntities("&amp;lt;b&amp;gt;")).toBe("&lt;b&gt;");
86
+ });
87
+ });
88
+
89
+ describe("htmlToPlainText", () => {
90
+ it("strips tags and decodes entities", () => {
91
+ expect(htmlToPlainText("<strong>Hello &amp; world</strong>")).toBe("Hello & world");
92
+ });
93
+
94
+ it("collapses whitespace from tag removal", () => {
95
+ expect(htmlToPlainText("<p>foo</p><p>bar</p>")).toBe("foo bar");
96
+ });
97
+
98
+ it("trims leading and trailing whitespace", () => {
99
+ expect(htmlToPlainText(" <span>hi</span> ")).toBe("hi");
100
+ });
101
+ });
102
+
103
+ describe("extractMSTeamsQuoteInfo", () => {
104
+ const replyAttachment = (overrides?: { content?: string; contentType?: string }) => ({
105
+ contentType: overrides?.contentType ?? "text/html",
106
+ content:
107
+ overrides?.content ??
108
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
109
+ '<strong itemprop="mri">Alice</strong>' +
110
+ '<p itemprop="copy">Hello world</p>' +
111
+ "</blockquote>",
112
+ });
113
+
114
+ it("extracts sender and body from a Teams reply attachment", () => {
115
+ const result = extractMSTeamsQuoteInfo([replyAttachment()]);
116
+ expect(result).toEqual({ sender: "Alice", body: "Hello world" });
117
+ });
118
+
119
+ it("returns undefined for empty attachments array", () => {
120
+ expect(extractMSTeamsQuoteInfo([])).toBeUndefined();
121
+ });
122
+
123
+ it("returns undefined when no reply blockquote is present", () => {
124
+ expect(
125
+ extractMSTeamsQuoteInfo([{ contentType: "text/html", content: "<p>just a message</p>" }]),
126
+ ).toBeUndefined();
127
+ });
128
+
129
+ it("uses 'unknown' as sender when sender element is absent", () => {
130
+ const result = extractMSTeamsQuoteInfo([
131
+ {
132
+ contentType: "text/html",
133
+ content:
134
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
135
+ '<p itemprop="copy">quoted text</p>' +
136
+ "</blockquote>",
137
+ },
138
+ ]);
139
+ expect(result).toEqual({ sender: "unknown", body: "quoted text" });
140
+ });
141
+
142
+ it("returns undefined when body element is absent", () => {
143
+ const result = extractMSTeamsQuoteInfo([
144
+ {
145
+ contentType: "text/html",
146
+ content:
147
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
148
+ '<strong itemprop="mri">Alice</strong>' +
149
+ "</blockquote>",
150
+ },
151
+ ]);
152
+ expect(result).toBeUndefined();
153
+ });
154
+
155
+ it("decodes HTML entities in body text", () => {
156
+ const result = extractMSTeamsQuoteInfo([
157
+ {
158
+ contentType: "text/html",
159
+ content:
160
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
161
+ '<strong itemprop="mri">Bob</strong>' +
162
+ '<p itemprop="copy">2 &lt; 3 &amp; 4 &gt; 1</p>' +
163
+ "</blockquote>",
164
+ },
165
+ ]);
166
+ expect(result).toEqual({ sender: "Bob", body: "2 < 3 & 4 > 1" });
167
+ });
168
+
169
+ it("handles multiline body by collapsing whitespace", () => {
170
+ const result = extractMSTeamsQuoteInfo([
171
+ {
172
+ contentType: "text/html",
173
+ content:
174
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
175
+ '<strong itemprop="mri">Carol</strong>' +
176
+ '<p itemprop="copy">line one\nline two</p>' +
177
+ "</blockquote>",
178
+ },
179
+ ]);
180
+ expect(result?.body).toBe("line one line two");
181
+ });
182
+
183
+ it("skips non-string content values", () => {
184
+ expect(
185
+ extractMSTeamsQuoteInfo([{ contentType: "application/json", content: { foo: "bar" } }]),
186
+ ).toBeUndefined();
187
+ });
188
+
189
+ it("handles object content with .text property containing the reply HTML", () => {
190
+ const htmlContent =
191
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
192
+ '<strong itemprop="mri">Dave</strong>' +
193
+ '<p itemprop="copy">hello from object</p>' +
194
+ "</blockquote>";
195
+ const result = extractMSTeamsQuoteInfo([
196
+ { contentType: "text/html", content: { text: htmlContent } },
197
+ ]);
198
+ expect(result).toEqual({ sender: "Dave", body: "hello from object" });
199
+ });
200
+
201
+ it("handles object content with .body property containing the reply HTML", () => {
202
+ const htmlContent =
203
+ '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' +
204
+ '<strong itemprop="mri">Eve</strong>' +
205
+ '<p itemprop="copy">hello from body field</p>' +
206
+ "</blockquote>";
207
+ const result = extractMSTeamsQuoteInfo([
208
+ { contentType: "text/html", content: { body: htmlContent } },
209
+ ]);
210
+ expect(result).toEqual({ sender: "Eve", body: "hello from body field" });
211
+ });
212
+
213
+ it("finds quote in second attachment when first has no quote", () => {
214
+ const result = extractMSTeamsQuoteInfo([
215
+ { contentType: "text/plain", content: "plain text" },
216
+ replyAttachment(),
217
+ ]);
218
+ expect(result).toEqual({ sender: "Alice", body: "Hello world" });
219
+ });
220
+ });
221
+ });
package/src/inbound.ts ADDED
@@ -0,0 +1,148 @@
1
+ type MSTeamsQuoteInfo = {
2
+ sender: string;
3
+ body: string;
4
+ };
5
+
6
+ /**
7
+ * Decode common HTML entities to plain text.
8
+ */
9
+ export function decodeHtmlEntities(html: string): string {
10
+ return html
11
+ .replace(/&lt;/g, "<")
12
+ .replace(/&gt;/g, ">")
13
+ .replace(/&quot;/g, '"')
14
+ .replace(/&#39;/g, "'")
15
+ .replace(/&#x27;/g, "'")
16
+ .replace(/&nbsp;/g, " ")
17
+ .replace(/&amp;/g, "&"); // must be last to prevent double-decoding (e.g. &amp;lt; → &lt; not <)
18
+ }
19
+
20
+ /**
21
+ * Strip HTML tags, preserving text content.
22
+ */
23
+ export function htmlToPlainText(html: string): string {
24
+ return decodeHtmlEntities(
25
+ html
26
+ .replace(/<[^>]*>/g, " ")
27
+ .replace(/\s+/g, " ")
28
+ .trim(),
29
+ );
30
+ }
31
+
32
+ /**
33
+ * Extract quote info from MS Teams HTML reply attachments.
34
+ * Teams wraps quoted content in a blockquote with itemtype="http://schema.skype.com/Reply".
35
+ */
36
+ export function extractMSTeamsQuoteInfo(
37
+ attachments: Array<{ contentType?: string | null; content?: unknown }>,
38
+ ): MSTeamsQuoteInfo | undefined {
39
+ for (const att of attachments) {
40
+ // Content may be a plain string or an object with .text/.body (e.g. Adaptive Card payloads).
41
+ let content = "";
42
+ if (typeof att.content === "string") {
43
+ content = att.content;
44
+ } else if (typeof att.content === "object" && att.content !== null) {
45
+ const record = att.content as Record<string, unknown>;
46
+ content =
47
+ typeof record.text === "string"
48
+ ? record.text
49
+ : typeof record.body === "string"
50
+ ? record.body
51
+ : "";
52
+ }
53
+ if (!content) {
54
+ continue;
55
+ }
56
+
57
+ // Look for the Skype Reply schema blockquote.
58
+ if (!content.includes("http://schema.skype.com/Reply")) {
59
+ continue;
60
+ }
61
+
62
+ // Extract sender from <strong itemprop="mri">.
63
+ const senderMatch = /<strong[^>]*itemprop=["']mri["'][^>]*>(.*?)<\/strong>/i.exec(content);
64
+ const sender = senderMatch?.[1] ? htmlToPlainText(senderMatch[1]) : undefined;
65
+
66
+ // Extract body from <p itemprop="copy">.
67
+ const bodyMatch = /<p[^>]*itemprop=["']copy["'][^>]*>(.*?)<\/p>/is.exec(content);
68
+ const body = bodyMatch?.[1] ? htmlToPlainText(bodyMatch[1]) : undefined;
69
+
70
+ if (body) {
71
+ return { sender: sender ?? "unknown", body };
72
+ }
73
+ }
74
+ return undefined;
75
+ }
76
+
77
+ type MentionableActivity = {
78
+ recipient?: { id?: string } | null;
79
+ entities?: Array<{
80
+ type?: string;
81
+ mentioned?: { id?: string };
82
+ }> | null;
83
+ };
84
+
85
+ export function normalizeMSTeamsConversationId(raw: string): string {
86
+ return raw.split(";")[0] ?? raw;
87
+ }
88
+
89
+ export function extractMSTeamsConversationMessageId(raw: string): string | undefined {
90
+ if (!raw) {
91
+ return undefined;
92
+ }
93
+ const match = /(?:^|;)messageid=([^;]+)/i.exec(raw);
94
+ const value = match?.[1]?.trim() ?? "";
95
+ return value || undefined;
96
+ }
97
+
98
+ export function parseMSTeamsActivityTimestamp(value: unknown): Date | undefined {
99
+ if (!value) {
100
+ return undefined;
101
+ }
102
+ if (value instanceof Date) {
103
+ return value;
104
+ }
105
+ if (typeof value !== "string") {
106
+ return undefined;
107
+ }
108
+ const date = new Date(value);
109
+ return Number.isNaN(date.getTime()) ? undefined : date;
110
+ }
111
+
112
+ export function stripMSTeamsMentionTags(text: string): string {
113
+ // Teams wraps mentions in <at>...</at> tags
114
+ return text.replace(/<at[^>]*>.*?<\/at>/gi, "").trim();
115
+ }
116
+
117
+ /**
118
+ * Bot Framework uses 'a:xxx' conversation IDs for personal chats, but Graph API
119
+ * requires the '19:{userId}_{botAppId}@unq.gbl.spaces' format.
120
+ *
121
+ * This is the documented Graph API format for 1:1 chat thread IDs between a user
122
+ * and a bot/app. See Microsoft docs "Get chat between user and app":
123
+ * https://learn.microsoft.com/en-us/graph/api/userscopeteamsappinstallation-get-chat
124
+ *
125
+ * The format is only synthesized when the Bot Framework conversation ID starts with
126
+ * 'a:' (the opaque format used by BF but not recognized by Graph). If the ID already
127
+ * has the '19:...' Graph format, it is passed through unchanged.
128
+ */
129
+ export function translateMSTeamsDmConversationIdForGraph(params: {
130
+ isDirectMessage: boolean;
131
+ conversationId: string;
132
+ aadObjectId?: string | null;
133
+ appId?: string | null;
134
+ }): string {
135
+ const { isDirectMessage, conversationId, aadObjectId, appId } = params;
136
+ return isDirectMessage && conversationId.startsWith("a:") && aadObjectId && appId
137
+ ? `19:${aadObjectId}_${appId}@unq.gbl.spaces`
138
+ : conversationId;
139
+ }
140
+
141
+ export function wasMSTeamsBotMentioned(activity: MentionableActivity): boolean {
142
+ const botId = activity.recipient?.id;
143
+ if (!botId) {
144
+ return false;
145
+ }
146
+ const entities = activity.entities ?? [];
147
+ return entities.some((e) => e.type === "mention" && e.mentioned?.id === botId);
148
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { monitorMSTeamsProvider } from "./monitor.js";
2
+ export { probeMSTeams } from "./probe.js";
3
+ export { sendMessageMSTeams, sendPollMSTeams } from "./send.js";
4
+ export { type MSTeamsCredentials, resolveMSTeamsCredentials } from "./token.js";