@openclaw/msteams 2026.5.2 → 2026.5.3-beta.1

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 (197) hide show
  1. package/dist/api.js +3 -0
  2. package/dist/channel-D7hdreTh.js +984 -0
  3. package/dist/channel-config-api.js +2 -0
  4. package/dist/channel-plugin-api.js +2 -0
  5. package/dist/channel.runtime-BC1ruIfN.js +573 -0
  6. package/dist/config-schema-B8QezH6t.js +15 -0
  7. package/dist/contract-api.js +2 -0
  8. package/dist/graph-users-9uQJepqr.js +1354 -0
  9. package/dist/index.js +22 -0
  10. package/dist/oauth-BWJyilR1.js +114 -0
  11. package/dist/oauth.token-xxpoLWy5.js +115 -0
  12. package/dist/policy-DTnU2GR7.js +142 -0
  13. package/dist/probe-D_H8yFps.js +2194 -0
  14. package/dist/resolve-allowlist-D41JSziq.js +219 -0
  15. package/dist/runtime-api-DV1iVMn1.js +28 -0
  16. package/dist/runtime-api.js +2 -0
  17. package/dist/secret-contract-BuoEXmPS.js +35 -0
  18. package/dist/secret-contract-api.js +2 -0
  19. package/dist/setup-entry.js +15 -0
  20. package/dist/setup-plugin-api.js +64 -0
  21. package/dist/setup-surface-BLkFQYIQ.js +313 -0
  22. package/dist/src-CFp1QpFd.js +4064 -0
  23. package/dist/test-api.js +2 -0
  24. package/package.json +14 -6
  25. package/api.ts +0 -3
  26. package/channel-config-api.ts +0 -1
  27. package/channel-plugin-api.ts +0 -2
  28. package/config-api.ts +0 -4
  29. package/contract-api.ts +0 -4
  30. package/index.ts +0 -20
  31. package/runtime-api.ts +0 -73
  32. package/secret-contract-api.ts +0 -5
  33. package/setup-entry.ts +0 -13
  34. package/setup-plugin-api.ts +0 -3
  35. package/src/ai-entity.ts +0 -7
  36. package/src/approval-auth.ts +0 -44
  37. package/src/attachments/bot-framework.test.ts +0 -461
  38. package/src/attachments/bot-framework.ts +0 -362
  39. package/src/attachments/download.ts +0 -311
  40. package/src/attachments/graph.test.ts +0 -416
  41. package/src/attachments/graph.ts +0 -484
  42. package/src/attachments/html.ts +0 -122
  43. package/src/attachments/payload.ts +0 -14
  44. package/src/attachments/remote-media.test.ts +0 -137
  45. package/src/attachments/remote-media.ts +0 -112
  46. package/src/attachments/shared.test.ts +0 -530
  47. package/src/attachments/shared.ts +0 -626
  48. package/src/attachments/types.ts +0 -47
  49. package/src/attachments.graph.test.ts +0 -342
  50. package/src/attachments.helpers.test.ts +0 -246
  51. package/src/attachments.test-helpers.ts +0 -17
  52. package/src/attachments.test.ts +0 -687
  53. package/src/attachments.ts +0 -18
  54. package/src/block-streaming-config.test.ts +0 -61
  55. package/src/channel-api.ts +0 -1
  56. package/src/channel.actions.test.ts +0 -742
  57. package/src/channel.directory.test.ts +0 -200
  58. package/src/channel.runtime.ts +0 -56
  59. package/src/channel.setup.ts +0 -77
  60. package/src/channel.test.ts +0 -128
  61. package/src/channel.ts +0 -1136
  62. package/src/config-schema.ts +0 -6
  63. package/src/config-ui-hints.ts +0 -12
  64. package/src/conversation-store-fs.test.ts +0 -74
  65. package/src/conversation-store-fs.ts +0 -149
  66. package/src/conversation-store-helpers.test.ts +0 -202
  67. package/src/conversation-store-helpers.ts +0 -105
  68. package/src/conversation-store-memory.ts +0 -51
  69. package/src/conversation-store.shared.test.ts +0 -225
  70. package/src/conversation-store.ts +0 -71
  71. package/src/directory-live.test.ts +0 -156
  72. package/src/directory-live.ts +0 -111
  73. package/src/doctor.ts +0 -27
  74. package/src/errors.test.ts +0 -133
  75. package/src/errors.ts +0 -246
  76. package/src/feedback-reflection-prompt.ts +0 -117
  77. package/src/feedback-reflection-store.ts +0 -114
  78. package/src/feedback-reflection.test.ts +0 -237
  79. package/src/feedback-reflection.ts +0 -283
  80. package/src/file-consent-helpers.test.ts +0 -326
  81. package/src/file-consent-helpers.ts +0 -126
  82. package/src/file-consent-invoke.ts +0 -150
  83. package/src/file-consent.test.ts +0 -363
  84. package/src/file-consent.ts +0 -287
  85. package/src/graph-chat.ts +0 -55
  86. package/src/graph-group-management.test.ts +0 -318
  87. package/src/graph-group-management.ts +0 -168
  88. package/src/graph-members.test.ts +0 -89
  89. package/src/graph-members.ts +0 -48
  90. package/src/graph-messages.actions.test.ts +0 -243
  91. package/src/graph-messages.read.test.ts +0 -391
  92. package/src/graph-messages.search.test.ts +0 -213
  93. package/src/graph-messages.test-helpers.ts +0 -50
  94. package/src/graph-messages.ts +0 -534
  95. package/src/graph-teams.test.ts +0 -215
  96. package/src/graph-teams.ts +0 -114
  97. package/src/graph-thread.test.ts +0 -246
  98. package/src/graph-thread.ts +0 -146
  99. package/src/graph-upload.test.ts +0 -258
  100. package/src/graph-upload.ts +0 -531
  101. package/src/graph-users.ts +0 -29
  102. package/src/graph.test.ts +0 -516
  103. package/src/graph.ts +0 -293
  104. package/src/inbound.test.ts +0 -221
  105. package/src/inbound.ts +0 -148
  106. package/src/index.ts +0 -4
  107. package/src/media-helpers.test.ts +0 -202
  108. package/src/media-helpers.ts +0 -105
  109. package/src/mentions.test.ts +0 -244
  110. package/src/mentions.ts +0 -114
  111. package/src/messenger.test.ts +0 -865
  112. package/src/messenger.ts +0 -605
  113. package/src/monitor-handler/access.ts +0 -125
  114. package/src/monitor-handler/inbound-media.test.ts +0 -289
  115. package/src/monitor-handler/inbound-media.ts +0 -180
  116. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  117. package/src/monitor-handler/message-handler.authz.test.ts +0 -669
  118. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  119. package/src/monitor-handler/message-handler.test-support.ts +0 -100
  120. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -223
  121. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -77
  122. package/src/monitor-handler/message-handler.ts +0 -1000
  123. package/src/monitor-handler/reaction-handler.test.ts +0 -267
  124. package/src/monitor-handler/reaction-handler.ts +0 -210
  125. package/src/monitor-handler/thread-session.ts +0 -17
  126. package/src/monitor-handler.adaptive-card.test.ts +0 -162
  127. package/src/monitor-handler.feedback-authz.test.ts +0 -314
  128. package/src/monitor-handler.file-consent.test.ts +0 -423
  129. package/src/monitor-handler.sso.test.ts +0 -563
  130. package/src/monitor-handler.test-helpers.ts +0 -180
  131. package/src/monitor-handler.ts +0 -534
  132. package/src/monitor-handler.types.ts +0 -27
  133. package/src/monitor-types.ts +0 -6
  134. package/src/monitor.lifecycle.test.ts +0 -278
  135. package/src/monitor.test.ts +0 -119
  136. package/src/monitor.ts +0 -442
  137. package/src/oauth.flow.ts +0 -77
  138. package/src/oauth.shared.ts +0 -37
  139. package/src/oauth.test.ts +0 -305
  140. package/src/oauth.token.ts +0 -158
  141. package/src/oauth.ts +0 -130
  142. package/src/outbound.test.ts +0 -130
  143. package/src/outbound.ts +0 -71
  144. package/src/pending-uploads-fs.test.ts +0 -246
  145. package/src/pending-uploads-fs.ts +0 -235
  146. package/src/pending-uploads.test.ts +0 -173
  147. package/src/pending-uploads.ts +0 -121
  148. package/src/policy.test.ts +0 -240
  149. package/src/policy.ts +0 -262
  150. package/src/polls-store-memory.ts +0 -32
  151. package/src/polls.test.ts +0 -160
  152. package/src/polls.ts +0 -323
  153. package/src/presentation.ts +0 -68
  154. package/src/probe.test.ts +0 -77
  155. package/src/probe.ts +0 -132
  156. package/src/reply-dispatcher.test.ts +0 -437
  157. package/src/reply-dispatcher.ts +0 -346
  158. package/src/reply-stream-controller.test.ts +0 -235
  159. package/src/reply-stream-controller.ts +0 -147
  160. package/src/resolve-allowlist.test.ts +0 -250
  161. package/src/resolve-allowlist.ts +0 -309
  162. package/src/revoked-context.ts +0 -17
  163. package/src/runtime.ts +0 -9
  164. package/src/sdk-types.ts +0 -59
  165. package/src/sdk.test.ts +0 -666
  166. package/src/sdk.ts +0 -884
  167. package/src/secret-contract.ts +0 -49
  168. package/src/secret-input.ts +0 -7
  169. package/src/send-context.ts +0 -231
  170. package/src/send.test.ts +0 -493
  171. package/src/send.ts +0 -637
  172. package/src/sent-message-cache.test.ts +0 -15
  173. package/src/sent-message-cache.ts +0 -56
  174. package/src/session-route.ts +0 -40
  175. package/src/setup-core.ts +0 -160
  176. package/src/setup-surface.test.ts +0 -202
  177. package/src/setup-surface.ts +0 -320
  178. package/src/sso-token-store.test.ts +0 -72
  179. package/src/sso-token-store.ts +0 -166
  180. package/src/sso.ts +0 -300
  181. package/src/storage.ts +0 -25
  182. package/src/store-fs.ts +0 -44
  183. package/src/streaming-message.test.ts +0 -262
  184. package/src/streaming-message.ts +0 -297
  185. package/src/test-runtime.ts +0 -16
  186. package/src/thread-parent-context.test.ts +0 -224
  187. package/src/thread-parent-context.ts +0 -159
  188. package/src/token-response.ts +0 -11
  189. package/src/token.test.ts +0 -259
  190. package/src/token.ts +0 -195
  191. package/src/user-agent.test.ts +0 -86
  192. package/src/user-agent.ts +0 -53
  193. package/src/webhook-timeouts.ts +0 -27
  194. package/src/welcome-card.test.ts +0 -81
  195. package/src/welcome-card.ts +0 -57
  196. package/test-api.ts +0 -1
  197. package/tsconfig.json +0 -16
@@ -1,484 +0,0 @@
1
- import { fetchWithSsrFGuard, type SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
2
- import {
3
- normalizeLowercaseStringOrEmpty,
4
- normalizeOptionalLowercaseString,
5
- normalizeOptionalString,
6
- } from "openclaw/plugin-sdk/text-runtime";
7
- import { getMSTeamsRuntime } from "../runtime.js";
8
- import { ensureUserAgentHeader } from "../user-agent.js";
9
- import { downloadMSTeamsAttachments } from "./download.js";
10
- import { downloadAndStoreMSTeamsRemoteMedia } from "./remote-media.js";
11
- import {
12
- applyAuthorizationHeaderForUrl,
13
- encodeGraphShareId,
14
- GRAPH_ROOT,
15
- estimateBase64DecodedBytes,
16
- inferPlaceholder,
17
- readNestedString,
18
- isUrlAllowed,
19
- type MSTeamsAttachmentDownloadLogger,
20
- type MSTeamsAttachmentFetchPolicy,
21
- type MSTeamsAttachmentResolveFn,
22
- normalizeContentType,
23
- resolveMediaSsrfPolicy,
24
- resolveAttachmentFetchPolicy,
25
- resolveRequestUrl,
26
- safeFetchWithPolicy,
27
- } from "./shared.js";
28
- import type {
29
- MSTeamsAccessTokenProvider,
30
- MSTeamsAttachmentLike,
31
- MSTeamsGraphMediaLogger,
32
- MSTeamsGraphMediaResult,
33
- MSTeamsInboundMedia,
34
- } from "./types.js";
35
-
36
- type GraphHostedContent = {
37
- id?: string | null;
38
- contentType?: string | null;
39
- contentBytes?: string | null;
40
- };
41
-
42
- type GraphAttachment = {
43
- id?: string | null;
44
- contentType?: string | null;
45
- contentUrl?: string | null;
46
- name?: string | null;
47
- thumbnailUrl?: string | null;
48
- content?: unknown;
49
- };
50
-
51
- export function buildMSTeamsGraphMessageUrls(params: {
52
- conversationType?: string | null;
53
- conversationId?: string | null;
54
- messageId?: string | null;
55
- replyToId?: string | null;
56
- conversationMessageId?: string | null;
57
- channelData?: unknown;
58
- }): string[] {
59
- const conversationType = normalizeLowercaseStringOrEmpty(params.conversationType ?? "");
60
- const messageIdCandidates = new Set<string>();
61
- const pushCandidate = (value: string | null | undefined) => {
62
- const trimmed = normalizeOptionalString(value) ?? "";
63
- if (trimmed) {
64
- messageIdCandidates.add(trimmed);
65
- }
66
- };
67
-
68
- pushCandidate(params.messageId);
69
- pushCandidate(params.conversationMessageId);
70
- pushCandidate(readNestedString(params.channelData, ["messageId"]));
71
- pushCandidate(readNestedString(params.channelData, ["teamsMessageId"]));
72
-
73
- const replyToId = normalizeOptionalString(params.replyToId) ?? "";
74
-
75
- if (conversationType === "channel") {
76
- const teamId =
77
- readNestedString(params.channelData, ["team", "id"]) ??
78
- readNestedString(params.channelData, ["teamId"]);
79
- const channelId =
80
- readNestedString(params.channelData, ["channel", "id"]) ??
81
- readNestedString(params.channelData, ["channelId"]) ??
82
- readNestedString(params.channelData, ["teamsChannelId"]);
83
- if (!teamId || !channelId) {
84
- return [];
85
- }
86
- const urls: string[] = [];
87
- if (replyToId) {
88
- for (const candidate of messageIdCandidates) {
89
- if (candidate === replyToId) {
90
- continue;
91
- }
92
- urls.push(
93
- `${GRAPH_ROOT}/teams/${encodeURIComponent(teamId)}/channels/${encodeURIComponent(channelId)}/messages/${encodeURIComponent(replyToId)}/replies/${encodeURIComponent(candidate)}`,
94
- );
95
- }
96
- }
97
- if (messageIdCandidates.size === 0 && replyToId) {
98
- messageIdCandidates.add(replyToId);
99
- }
100
- for (const candidate of messageIdCandidates) {
101
- urls.push(
102
- `${GRAPH_ROOT}/teams/${encodeURIComponent(teamId)}/channels/${encodeURIComponent(channelId)}/messages/${encodeURIComponent(candidate)}`,
103
- );
104
- }
105
- return Array.from(new Set(urls));
106
- }
107
-
108
- const chatId = params.conversationId?.trim() || readNestedString(params.channelData, ["chatId"]);
109
- if (!chatId) {
110
- return [];
111
- }
112
- if (messageIdCandidates.size === 0 && replyToId) {
113
- messageIdCandidates.add(replyToId);
114
- }
115
- const urls = Array.from(messageIdCandidates).map(
116
- (candidate) =>
117
- `${GRAPH_ROOT}/chats/${encodeURIComponent(chatId)}/messages/${encodeURIComponent(candidate)}`,
118
- );
119
- return Array.from(new Set(urls));
120
- }
121
-
122
- async function fetchGraphCollection(params: {
123
- url: string;
124
- accessToken: string;
125
- fetchFn?: typeof fetch;
126
- ssrfPolicy?: SsrFPolicy;
127
- }): Promise<{ status: number; items: unknown[] }> {
128
- const fetchFn = params.fetchFn ?? fetch;
129
- const { response, release } = await fetchWithSsrFGuard({
130
- url: params.url,
131
- fetchImpl: fetchFn,
132
- init: {
133
- headers: ensureUserAgentHeader({ Authorization: `Bearer ${params.accessToken}` }),
134
- },
135
- policy: params.ssrfPolicy,
136
- auditContext: "msteams.graph.collection",
137
- });
138
- try {
139
- const status = response.status;
140
- if (!response.ok) {
141
- return { status, items: [] };
142
- }
143
- try {
144
- const data = (await response.json()) as { value?: unknown[] };
145
- return { status, items: Array.isArray(data.value) ? data.value : [] };
146
- } catch {
147
- return { status, items: [] };
148
- }
149
- } finally {
150
- await release();
151
- }
152
- }
153
-
154
- function normalizeGraphAttachment(att: GraphAttachment): MSTeamsAttachmentLike {
155
- let content: unknown = att.content;
156
- if (typeof content === "string") {
157
- try {
158
- content = JSON.parse(content);
159
- } catch {
160
- // Keep as raw string if it's not JSON.
161
- }
162
- }
163
- return {
164
- contentType: normalizeContentType(att.contentType) ?? undefined,
165
- contentUrl: att.contentUrl ?? undefined,
166
- name: att.name ?? undefined,
167
- thumbnailUrl: att.thumbnailUrl ?? undefined,
168
- content,
169
- };
170
- }
171
-
172
- /**
173
- * Download all hosted content from a Teams message (images, documents, etc.).
174
- * Renamed from downloadGraphHostedImages to support all file types.
175
- */
176
- async function downloadGraphHostedContent(params: {
177
- accessToken: string;
178
- messageUrl: string;
179
- maxBytes: number;
180
- fetchFn?: typeof fetch;
181
- preserveFilenames?: boolean;
182
- ssrfPolicy?: SsrFPolicy;
183
- logger?: MSTeamsAttachmentDownloadLogger;
184
- }): Promise<{ media: MSTeamsInboundMedia[]; status: number; count: number }> {
185
- const hosted = (await fetchGraphCollection({
186
- url: `${params.messageUrl}/hostedContents`,
187
- accessToken: params.accessToken,
188
- fetchFn: params.fetchFn,
189
- ssrfPolicy: params.ssrfPolicy,
190
- })) as { status: number; items: GraphHostedContent[] };
191
- if (hosted.items.length === 0) {
192
- return { media: [], status: hosted.status, count: 0 };
193
- }
194
-
195
- const out: MSTeamsInboundMedia[] = [];
196
- for (const item of hosted.items) {
197
- const contentBytes = typeof item.contentBytes === "string" ? item.contentBytes : "";
198
- let buffer: Buffer;
199
- if (contentBytes) {
200
- if (estimateBase64DecodedBytes(contentBytes) > params.maxBytes) {
201
- continue;
202
- }
203
- try {
204
- buffer = Buffer.from(contentBytes, "base64");
205
- } catch (err) {
206
- params.logger?.warn?.("msteams graph hostedContent base64 decode failed", {
207
- error: err instanceof Error ? err.message : String(err),
208
- });
209
- continue;
210
- }
211
- } else if (item.id) {
212
- // contentBytes not inline — fetch from the individual $value endpoint.
213
- try {
214
- const valueUrl = `${params.messageUrl}/hostedContents/${encodeURIComponent(item.id)}/$value`;
215
- const { response: valRes, release } = await fetchWithSsrFGuard({
216
- url: valueUrl,
217
- fetchImpl: params.fetchFn ?? fetch,
218
- init: {
219
- headers: ensureUserAgentHeader({ Authorization: `Bearer ${params.accessToken}` }),
220
- },
221
- policy: params.ssrfPolicy,
222
- auditContext: "msteams.graph.hostedContent.value",
223
- });
224
- try {
225
- if (!valRes.ok) {
226
- continue;
227
- }
228
- // Check Content-Length before buffering to avoid RSS spikes on large files.
229
- const cl = valRes.headers.get("content-length");
230
- if (cl && Number(cl) > params.maxBytes) {
231
- continue;
232
- }
233
- const ab = await valRes.arrayBuffer();
234
- buffer = Buffer.from(ab);
235
- } finally {
236
- await release();
237
- }
238
- } catch (err) {
239
- params.logger?.warn?.("msteams graph hostedContent value fetch failed", {
240
- error: err instanceof Error ? err.message : String(err),
241
- });
242
- continue;
243
- }
244
- } else {
245
- continue;
246
- }
247
- if (buffer.byteLength > params.maxBytes) {
248
- continue;
249
- }
250
- const mime = await getMSTeamsRuntime().media.detectMime({
251
- buffer,
252
- headerMime: item.contentType ?? undefined,
253
- });
254
- // Download any file type, not just images
255
- try {
256
- const saved = await getMSTeamsRuntime().channel.media.saveMediaBuffer(
257
- buffer,
258
- mime ?? item.contentType ?? undefined,
259
- "inbound",
260
- params.maxBytes,
261
- );
262
- out.push({
263
- path: saved.path,
264
- contentType: saved.contentType,
265
- placeholder: inferPlaceholder({ contentType: saved.contentType }),
266
- });
267
- } catch (err) {
268
- params.logger?.warn?.("msteams graph hostedContent save failed", {
269
- error: err instanceof Error ? err.message : String(err),
270
- });
271
- }
272
- }
273
-
274
- return { media: out, status: hosted.status, count: hosted.items.length };
275
- }
276
-
277
- export async function downloadMSTeamsGraphMedia(params: {
278
- messageUrl?: string | null;
279
- tokenProvider?: MSTeamsAccessTokenProvider;
280
- maxBytes: number;
281
- allowHosts?: string[];
282
- authAllowHosts?: string[];
283
- fetchFn?: typeof fetch;
284
- resolveFn?: MSTeamsAttachmentResolveFn;
285
- /** When true, embeds original filename in stored path for later extraction. */
286
- preserveFilenames?: boolean;
287
- /** Optional logger used to surface Graph/SharePoint fetch errors. */
288
- logger?: MSTeamsAttachmentDownloadLogger;
289
- /** Back-compat diagnostic logger used by older tests/callers. */
290
- log?: MSTeamsGraphMediaLogger;
291
- }): Promise<MSTeamsGraphMediaResult> {
292
- if (!params.messageUrl || !params.tokenProvider) {
293
- return { media: [] };
294
- }
295
- const policy: MSTeamsAttachmentFetchPolicy = resolveAttachmentFetchPolicy({
296
- allowHosts: params.allowHosts,
297
- authAllowHosts: params.authAllowHosts,
298
- });
299
- const ssrfPolicy = resolveMediaSsrfPolicy(policy.allowHosts);
300
- const messageUrl = params.messageUrl;
301
- const debugLog =
302
- params.log ?? (params.logger as MSTeamsGraphMediaLogger | undefined) ?? undefined;
303
- let accessToken: string;
304
- try {
305
- accessToken = await params.tokenProvider.getAccessToken("https://graph.microsoft.com");
306
- } catch (err) {
307
- debugLog?.debug?.("graph media token acquisition failed", {
308
- messageUrl,
309
- error: err instanceof Error ? err.message : String(err),
310
- });
311
- params.logger?.warn?.("msteams graph token acquisition failed", {
312
- error: err instanceof Error ? err.message : String(err),
313
- });
314
- return { media: [], messageUrl, tokenError: true };
315
- }
316
-
317
- const fetchFn = params.fetchFn ?? fetch;
318
- const sharePointMedia: MSTeamsInboundMedia[] = [];
319
- const downloadedReferenceUrls = new Set<string>();
320
- let messageAttachments: GraphAttachment[] = [];
321
- let messageStatus: number | undefined;
322
- try {
323
- const { response: msgRes, release } = await fetchWithSsrFGuard({
324
- url: messageUrl,
325
- fetchImpl: fetchFn,
326
- init: {
327
- headers: ensureUserAgentHeader({ Authorization: `Bearer ${accessToken}` }),
328
- },
329
- policy: ssrfPolicy,
330
- auditContext: "msteams.graph.message",
331
- });
332
- try {
333
- messageStatus = msgRes.status;
334
- if (msgRes.ok) {
335
- let msgData: {
336
- body?: { content?: string; contentType?: string };
337
- attachments?: GraphAttachment[];
338
- };
339
- try {
340
- msgData = (await msgRes.json()) as typeof msgData;
341
- } catch (err) {
342
- debugLog?.debug?.("graph media message parse failed", {
343
- messageUrl,
344
- error: err instanceof Error ? err.message : String(err),
345
- });
346
- params.logger?.warn?.("msteams graph message parse failed", {
347
- error: err instanceof Error ? err.message : String(err),
348
- messageUrl,
349
- });
350
- msgData = {};
351
- }
352
- messageAttachments = Array.isArray(msgData.attachments) ? msgData.attachments : [];
353
-
354
- const spAttachments = messageAttachments.filter(
355
- (a) => a.contentType === "reference" && a.contentUrl && a.name,
356
- );
357
- for (const att of spAttachments) {
358
- const name = att.name ?? "file";
359
- const shareUrl = att.contentUrl ?? "";
360
- if (!shareUrl) {
361
- continue;
362
- }
363
-
364
- try {
365
- const sharesUrl = `${GRAPH_ROOT}/shares/${encodeGraphShareId(shareUrl)}/driveItem/content`;
366
- if (!isUrlAllowed(sharesUrl, policy.allowHosts)) {
367
- debugLog?.debug?.("graph media sharepoint url not in allowHosts", {
368
- messageUrl,
369
- sharesUrl,
370
- });
371
- continue;
372
- }
373
-
374
- const media = await downloadAndStoreMSTeamsRemoteMedia({
375
- url: sharesUrl,
376
- filePathHint: name,
377
- maxBytes: params.maxBytes,
378
- contentTypeHint: "application/octet-stream",
379
- preserveFilenames: params.preserveFilenames,
380
- ssrfPolicy,
381
- useDirectFetch: true,
382
- fetchImpl: async (input, init) => {
383
- const requestUrl = resolveRequestUrl(input);
384
- const headers = ensureUserAgentHeader(init?.headers);
385
- applyAuthorizationHeaderForUrl({
386
- headers,
387
- url: requestUrl,
388
- authAllowHosts: policy.authAllowHosts,
389
- bearerToken: accessToken,
390
- });
391
- return await safeFetchWithPolicy({
392
- url: requestUrl,
393
- policy,
394
- fetchFn,
395
- requestInit: {
396
- ...init,
397
- headers,
398
- },
399
- resolveFn: params.resolveFn,
400
- });
401
- },
402
- });
403
- sharePointMedia.push(media);
404
- downloadedReferenceUrls.add(shareUrl);
405
- } catch (err) {
406
- params.logger?.warn?.("msteams SharePoint reference download failed", {
407
- error: err instanceof Error ? err.message : String(err),
408
- name,
409
- });
410
- }
411
- }
412
- } else {
413
- debugLog?.debug?.("graph media message fetch not ok", {
414
- messageUrl,
415
- status: messageStatus,
416
- });
417
- }
418
- } finally {
419
- await release();
420
- }
421
- } catch (err) {
422
- debugLog?.debug?.("graph media message fetch failed", {
423
- messageUrl,
424
- error: err instanceof Error ? err.message : String(err),
425
- });
426
- params.logger?.warn?.("msteams graph message fetch failed", {
427
- error: err instanceof Error ? err.message : String(err),
428
- });
429
- }
430
-
431
- const hosted = await downloadGraphHostedContent({
432
- accessToken,
433
- messageUrl,
434
- maxBytes: params.maxBytes,
435
- fetchFn: params.fetchFn,
436
- preserveFilenames: params.preserveFilenames,
437
- ssrfPolicy,
438
- logger: params.logger,
439
- });
440
-
441
- const normalizedAttachments = messageAttachments.map(normalizeGraphAttachment);
442
- const filteredAttachments =
443
- sharePointMedia.length > 0
444
- ? normalizedAttachments.filter((att) => {
445
- const contentType = normalizeOptionalLowercaseString(att.contentType);
446
- if (contentType !== "reference") {
447
- return true;
448
- }
449
- const url = typeof att.contentUrl === "string" ? att.contentUrl : "";
450
- if (!url) {
451
- return true;
452
- }
453
- return !downloadedReferenceUrls.has(url);
454
- })
455
- : normalizedAttachments;
456
- let attachmentMedia: MSTeamsInboundMedia[] = [];
457
- try {
458
- attachmentMedia = await downloadMSTeamsAttachments({
459
- attachments: filteredAttachments,
460
- maxBytes: params.maxBytes,
461
- tokenProvider: params.tokenProvider,
462
- allowHosts: policy.allowHosts,
463
- authAllowHosts: policy.authAllowHosts,
464
- fetchFn: params.fetchFn,
465
- resolveFn: params.resolveFn,
466
- preserveFilenames: params.preserveFilenames,
467
- logger: params.logger,
468
- });
469
- } catch (err) {
470
- params.logger?.warn?.("msteams graph attachment download failed", {
471
- error: err instanceof Error ? err.message : String(err),
472
- messageUrl,
473
- });
474
- }
475
-
476
- return {
477
- media: [...sharePointMedia, ...hosted.media, ...attachmentMedia],
478
- hostedCount: hosted.count,
479
- attachmentCount: filteredAttachments.length + sharePointMedia.length,
480
- hostedStatus: hosted.status,
481
- attachmentStatus: messageStatus,
482
- messageUrl,
483
- };
484
- }
@@ -1,122 +0,0 @@
1
- import {
2
- ATTACHMENT_TAG_RE,
3
- extractHtmlFromAttachment,
4
- extractInlineImageCandidates,
5
- IMG_SRC_RE,
6
- isLikelyImageAttachment,
7
- safeHostForUrl,
8
- } from "./shared.js";
9
- import type { MSTeamsAttachmentLike, MSTeamsHtmlAttachmentSummary } from "./types.js";
10
-
11
- /**
12
- * Extract every `<attachment id="...">` reference from the HTML attachments in
13
- * the inbound activity. Returns the complete (non-sliced) list; callers that
14
- * need a capped diagnostic summary can truncate after calling this helper.
15
- */
16
- export function extractMSTeamsHtmlAttachmentIds(
17
- attachments: MSTeamsAttachmentLike[] | undefined,
18
- ): string[] {
19
- const list = Array.isArray(attachments) ? attachments : [];
20
- if (list.length === 0) {
21
- return [];
22
- }
23
- const ids = new Set<string>();
24
- for (const att of list) {
25
- const html = extractHtmlFromAttachment(att);
26
- if (!html) {
27
- continue;
28
- }
29
- ATTACHMENT_TAG_RE.lastIndex = 0;
30
- let match: RegExpExecArray | null = ATTACHMENT_TAG_RE.exec(html);
31
- while (match) {
32
- const id = match[1]?.trim();
33
- if (id) {
34
- ids.add(id);
35
- }
36
- match = ATTACHMENT_TAG_RE.exec(html);
37
- }
38
- }
39
- return Array.from(ids);
40
- }
41
-
42
- export function summarizeMSTeamsHtmlAttachments(
43
- attachments: MSTeamsAttachmentLike[] | undefined,
44
- ): MSTeamsHtmlAttachmentSummary | undefined {
45
- const list = Array.isArray(attachments) ? attachments : [];
46
- if (list.length === 0) {
47
- return undefined;
48
- }
49
- let htmlAttachments = 0;
50
- let imgTags = 0;
51
- let dataImages = 0;
52
- let cidImages = 0;
53
- const srcHosts = new Set<string>();
54
- let attachmentTags = 0;
55
- const attachmentIds = new Set<string>();
56
-
57
- for (const att of list) {
58
- const html = extractHtmlFromAttachment(att);
59
- if (!html) {
60
- continue;
61
- }
62
- htmlAttachments += 1;
63
- IMG_SRC_RE.lastIndex = 0;
64
- let match: RegExpExecArray | null = IMG_SRC_RE.exec(html);
65
- while (match) {
66
- imgTags += 1;
67
- const src = match[1]?.trim();
68
- if (src) {
69
- if (src.startsWith("data:")) {
70
- dataImages += 1;
71
- } else if (src.startsWith("cid:")) {
72
- cidImages += 1;
73
- } else {
74
- srcHosts.add(safeHostForUrl(src));
75
- }
76
- }
77
- match = IMG_SRC_RE.exec(html);
78
- }
79
-
80
- ATTACHMENT_TAG_RE.lastIndex = 0;
81
- let attachmentMatch: RegExpExecArray | null = ATTACHMENT_TAG_RE.exec(html);
82
- while (attachmentMatch) {
83
- attachmentTags += 1;
84
- const id = attachmentMatch[1]?.trim();
85
- if (id) {
86
- attachmentIds.add(id);
87
- }
88
- attachmentMatch = ATTACHMENT_TAG_RE.exec(html);
89
- }
90
- }
91
-
92
- if (htmlAttachments === 0) {
93
- return undefined;
94
- }
95
- return {
96
- htmlAttachments,
97
- imgTags,
98
- dataImages,
99
- cidImages,
100
- srcHosts: Array.from(srcHosts).slice(0, 5),
101
- attachmentTags,
102
- attachmentIds: Array.from(attachmentIds).slice(0, 5),
103
- };
104
- }
105
-
106
- export function buildMSTeamsAttachmentPlaceholder(
107
- attachments: MSTeamsAttachmentLike[] | undefined,
108
- limits?: { maxInlineBytes?: number; maxInlineTotalBytes?: number },
109
- ): string {
110
- const list = Array.isArray(attachments) ? attachments : [];
111
- if (list.length === 0) {
112
- return "";
113
- }
114
- const imageCount = list.filter(isLikelyImageAttachment).length;
115
- const inlineCount = extractInlineImageCandidates(list, limits).length;
116
- const totalImages = imageCount + inlineCount;
117
- if (totalImages > 0) {
118
- return `<media:image>${totalImages > 1 ? ` (${totalImages} images)` : ""}`;
119
- }
120
- const count = list.length;
121
- return `<media:document>${count > 1 ? ` (${count} files)` : ""}`;
122
- }
@@ -1,14 +0,0 @@
1
- import { buildMediaPayload } from "../../runtime-api.js";
2
-
3
- export function buildMSTeamsMediaPayload(
4
- mediaList: Array<{ path: string; contentType?: string }>,
5
- ): {
6
- MediaPath?: string;
7
- MediaType?: string;
8
- MediaUrl?: string;
9
- MediaPaths?: string[];
10
- MediaUrls?: string[];
11
- MediaTypes?: string[];
12
- } {
13
- return buildMediaPayload(mediaList, { preserveMediaTypeCardinality: true });
14
- }