@kodelyth/msteams 2026.5.42 → 2026.6.2

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 (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +18 -6
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
@@ -1,136 +0,0 @@
1
- import {
2
- channelIngressRoutes,
3
- resolveStableChannelMessageIngress,
4
- type StableChannelIngressIdentityParams,
5
- } from "klaw/plugin-sdk/channel-ingress-runtime";
6
- import { normalizeOptionalLowercaseString } from "klaw/plugin-sdk/string-coerce-runtime";
7
- import {
8
- DEFAULT_ACCOUNT_ID,
9
- createChannelPairingController,
10
- isDangerousNameMatchingEnabled,
11
- resolveDefaultGroupPolicy,
12
- type KlawConfig,
13
- } from "../../runtime-api.js";
14
- import { normalizeMSTeamsConversationId } from "../inbound.js";
15
- import { resolveMSTeamsRouteConfig } from "../policy.js";
16
- import { getMSTeamsRuntime } from "../runtime.js";
17
- import type { MSTeamsTurnContext } from "../sdk-types.js";
18
-
19
- const MSTEAMS_SENDER_NAME_KIND = "plugin:msteams-sender-name" as const;
20
- const msteamsIngressIdentity = {
21
- key: "sender-id",
22
- normalize: normalizeIngressValue,
23
- aliases: [
24
- {
25
- key: "sender-name",
26
- kind: MSTEAMS_SENDER_NAME_KIND,
27
- normalizeEntry: normalizeIngressValue,
28
- normalizeSubject: normalizeIngressValue,
29
- dangerous: true,
30
- },
31
- ],
32
- isWildcardEntry: (entry) => normalizeIngressValue(entry) === "*",
33
- resolveEntryId: ({ entryIndex, fieldKey }) =>
34
- `msteams-entry-${entryIndex + 1}:${fieldKey === "sender-name" ? "name" : "id"}`,
35
- } satisfies StableChannelIngressIdentityParams;
36
-
37
- function normalizeIngressValue(value?: string | null): string | null {
38
- return normalizeOptionalLowercaseString(value) ?? null;
39
- }
40
-
41
- export async function resolveMSTeamsSenderAccess(params: {
42
- cfg: KlawConfig;
43
- activity: MSTeamsTurnContext["activity"];
44
- hasControlCommand?: boolean;
45
- }) {
46
- const activity = params.activity;
47
- const msteamsCfg = params.cfg.channels?.msteams;
48
- const conversationId = normalizeMSTeamsConversationId(activity.conversation?.id ?? "unknown");
49
- const convType = normalizeOptionalLowercaseString(activity.conversation?.conversationType);
50
- const isDirectMessage = convType === "personal" || (!convType && !activity.conversation?.isGroup);
51
- const senderId = activity.from?.aadObjectId ?? activity.from?.id ?? "unknown";
52
- const senderName = activity.from?.name ?? activity.from?.id ?? senderId;
53
-
54
- const core = getMSTeamsRuntime();
55
- const pairing = createChannelPairingController({
56
- core,
57
- channel: "msteams",
58
- accountId: DEFAULT_ACCOUNT_ID,
59
- });
60
- const dmPolicy = msteamsCfg?.dmPolicy ?? "pairing";
61
- const configuredDmAllowFrom = msteamsCfg?.allowFrom ?? [];
62
- const groupAllowFrom = msteamsCfg?.groupAllowFrom;
63
- const defaultGroupPolicy = resolveDefaultGroupPolicy(params.cfg);
64
- const groupPolicy =
65
- !isDirectMessage && msteamsCfg
66
- ? (msteamsCfg.groupPolicy ?? defaultGroupPolicy ?? "allowlist")
67
- : "disabled";
68
- const allowNameMatching = isDangerousNameMatchingEnabled(msteamsCfg);
69
- const channelGate = resolveMSTeamsRouteConfig({
70
- cfg: msteamsCfg,
71
- teamId: activity.channelData?.team?.id,
72
- teamName: activity.channelData?.team?.name,
73
- conversationId,
74
- channelName: activity.channelData?.channel?.name,
75
- allowNameMatching,
76
- });
77
-
78
- const resolved = await resolveStableChannelMessageIngress({
79
- channelId: "msteams",
80
- accountId: pairing.accountId,
81
- identity: msteamsIngressIdentity,
82
- cfg: params.cfg,
83
- readStoreAllowFrom: pairing.readAllowFromStore,
84
- subject: {
85
- stableId: senderId,
86
- aliases: { "sender-name": senderName },
87
- },
88
- conversation: {
89
- kind: isDirectMessage ? "direct" : convType === "channel" ? "channel" : "group",
90
- id: conversationId,
91
- parentId: activity.channelData?.team?.id,
92
- },
93
- route: channelIngressRoutes(
94
- !isDirectMessage &&
95
- channelGate.allowlistConfigured && {
96
- id: "msteams:team-channel",
97
- kind: "nestedAllowlist",
98
- allowed: channelGate.allowed,
99
- precedence: 0,
100
- matchId: "msteams-route",
101
- ...(channelGate.allowed && groupPolicy === "allowlist"
102
- ? {
103
- senderPolicy: "deny-when-empty" as const,
104
- senderAllowFromSource: "effective-group" as const,
105
- }
106
- : {}),
107
- },
108
- ),
109
- dmPolicy,
110
- groupPolicy,
111
- policy: {
112
- groupAllowFromFallbackToAllowFrom: true,
113
- mutableIdentifierMatching: allowNameMatching ? "enabled" : "disabled",
114
- },
115
- allowFrom: configuredDmAllowFrom,
116
- groupAllowFrom,
117
- command: {
118
- allowTextCommands: true,
119
- hasControlCommand: params.hasControlCommand === true,
120
- directGroupAllowFrom: isDirectMessage ? "effective" : "none",
121
- },
122
- });
123
- return {
124
- ...resolved,
125
- pairing,
126
- isDirectMessage,
127
- conversationId,
128
- senderId,
129
- senderName,
130
- msteamsCfg,
131
- dmPolicy,
132
- channelGate,
133
- allowNameMatching,
134
- groupPolicy,
135
- };
136
- }
@@ -1,314 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- vi.mock("../attachments.js", () => ({
4
- downloadMSTeamsAttachments: vi.fn(async () => []),
5
- downloadMSTeamsGraphMedia: vi.fn(async () => ({ media: [] })),
6
- downloadMSTeamsBotFrameworkAttachments: vi.fn(async () => ({ media: [], attachmentCount: 0 })),
7
- buildMSTeamsGraphMessageUrls: vi.fn(() => [
8
- "https://graph.microsoft.com/v1.0/chats/c/messages/m",
9
- ]),
10
- extractMSTeamsHtmlAttachmentIds: vi.fn(() => ["att-0", "att-1"]),
11
- isBotFrameworkPersonalChatId: vi.fn((id: string | null | undefined) => {
12
- if (typeof id !== "string") {
13
- return false;
14
- }
15
- return id.startsWith("a:") || id.startsWith("8:orgid:");
16
- }),
17
- }));
18
-
19
- import {
20
- buildMSTeamsGraphMessageUrls,
21
- downloadMSTeamsAttachments,
22
- downloadMSTeamsBotFrameworkAttachments,
23
- downloadMSTeamsGraphMedia,
24
- extractMSTeamsHtmlAttachmentIds,
25
- } from "../attachments.js";
26
- import { resolveMSTeamsInboundMedia } from "./inbound-media.js";
27
-
28
- const baseParams = {
29
- maxBytes: 1024 * 1024,
30
- tokenProvider: { getAccessToken: vi.fn(async () => "token") },
31
- conversationType: "personal",
32
- conversationId: "19:user_bot@unq.gbl.spaces",
33
- activity: { id: "msg-1", replyToId: undefined, channelData: {} },
34
- log: { debug: vi.fn() },
35
- };
36
-
37
- function firstGraphMediaCall() {
38
- const [call] = vi.mocked(downloadMSTeamsGraphMedia).mock.calls;
39
- if (!call) {
40
- throw new Error("expected Graph media download call");
41
- }
42
- return call[0];
43
- }
44
-
45
- function firstBotFrameworkAttachmentCall() {
46
- const [call] = vi.mocked(downloadMSTeamsBotFrameworkAttachments).mock.calls;
47
- if (!call) {
48
- throw new Error("expected Bot Framework attachment download call");
49
- }
50
- return call[0];
51
- }
52
-
53
- describe("resolveMSTeamsInboundMedia graph fallback trigger", () => {
54
- it("triggers Graph fallback when HTML contains <attachment> tags", async () => {
55
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
56
- vi.mocked(extractMSTeamsHtmlAttachmentIds).mockReturnValueOnce(["att-0"]);
57
- vi.mocked(downloadMSTeamsGraphMedia).mockResolvedValue({
58
- media: [{ path: "/tmp/img.png", contentType: "image/png", placeholder: "[image]" }],
59
- });
60
-
61
- await resolveMSTeamsInboundMedia({
62
- ...baseParams,
63
- attachments: [
64
- {
65
- contentType: "text/html",
66
- content: '<div>A file <attachment id="att-0"></attachment></div>',
67
- },
68
- ],
69
- });
70
-
71
- expect(buildMSTeamsGraphMessageUrls).toHaveBeenCalled();
72
- expect(downloadMSTeamsGraphMedia).toHaveBeenCalled();
73
- });
74
-
75
- it("does NOT trigger Graph fallback for mention-only HTML (no <attachment> tags)", async () => {
76
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
77
- // Mention cards include `<at>` markers but no `<attachment id="...">`,
78
- // so the extractor returns an empty ID list. The fallback must skip.
79
- vi.mocked(extractMSTeamsHtmlAttachmentIds).mockReturnValueOnce([]);
80
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
81
- vi.mocked(buildMSTeamsGraphMessageUrls).mockClear();
82
-
83
- await resolveMSTeamsInboundMedia({
84
- ...baseParams,
85
- attachments: [
86
- {
87
- contentType: "text/html",
88
- content: '<div><at id="0">Bot</at> hello there</div>',
89
- },
90
- ],
91
- });
92
-
93
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
94
- expect(buildMSTeamsGraphMessageUrls).not.toHaveBeenCalled();
95
- });
96
-
97
- it("does NOT trigger Graph fallback when no attachments are text/html", async () => {
98
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
99
- // No HTML attachments at all → extractor returns [].
100
- vi.mocked(extractMSTeamsHtmlAttachmentIds).mockReturnValueOnce([]);
101
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
102
- vi.mocked(buildMSTeamsGraphMessageUrls).mockClear();
103
-
104
- await resolveMSTeamsInboundMedia({
105
- ...baseParams,
106
- attachments: [
107
- { contentType: "image/png", contentUrl: "https://example.com/img.png" },
108
- { contentType: "application/pdf", contentUrl: "https://example.com/doc.pdf" },
109
- ],
110
- });
111
-
112
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
113
- });
114
-
115
- it("does NOT trigger Graph fallback when direct download succeeds", async () => {
116
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([
117
- { path: "/tmp/img.png", contentType: "image/png", placeholder: "[image]" },
118
- ]);
119
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
120
-
121
- await resolveMSTeamsInboundMedia({
122
- ...baseParams,
123
- attachments: [
124
- {
125
- contentType: "text/html",
126
- content: '<div><attachment id="att-0"></attachment></div>',
127
- },
128
- ],
129
- });
130
-
131
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
132
- });
133
-
134
- it("forwards log through to downloadMSTeamsGraphMedia for diagnostics", async () => {
135
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
136
- vi.mocked(extractMSTeamsHtmlAttachmentIds).mockReturnValueOnce(["att-0"]);
137
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
138
- vi.mocked(downloadMSTeamsGraphMedia).mockResolvedValue({ media: [] });
139
- const log = { debug: vi.fn() };
140
-
141
- await resolveMSTeamsInboundMedia({
142
- ...baseParams,
143
- log,
144
- attachments: [
145
- {
146
- contentType: "text/html",
147
- content: '<div><attachment id="att-0"></attachment></div>',
148
- },
149
- ],
150
- });
151
-
152
- const call = firstGraphMediaCall();
153
- // The monitor handler's logger is forwarded so graph.ts can report
154
- // message fetch failures instead of swallowing them (#51749).
155
- expect(call?.logger).toBe(log);
156
- expect(log.debug).toHaveBeenCalledWith("graph media fetch empty", {
157
- attempts: [
158
- {
159
- url: "https://graph.microsoft.com/v1.0/chats/c/messages/m",
160
- hostedStatus: undefined,
161
- attachmentStatus: undefined,
162
- hostedCount: undefined,
163
- attachmentCount: undefined,
164
- tokenError: undefined,
165
- },
166
- ],
167
- attachmentIdCount: 1,
168
- });
169
- });
170
- });
171
-
172
- describe("resolveMSTeamsInboundMedia bot framework DM routing", () => {
173
- const dmParams = {
174
- ...baseParams,
175
- conversationType: "personal",
176
- conversationId: "a:1dRsHCobZ1AxURzY05Dc",
177
- serviceUrl: "https://smba.trafficmanager.net/amer/",
178
- };
179
-
180
- it("routes 'a:' conversation IDs through the Bot Framework attachment endpoint", async () => {
181
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
182
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockClear();
183
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockResolvedValue({
184
- media: [
185
- {
186
- path: "/tmp/report.pdf",
187
- contentType: "application/pdf",
188
- placeholder: "<media:document>",
189
- },
190
- ],
191
- attachmentCount: 1,
192
- });
193
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
194
-
195
- const mediaList = await resolveMSTeamsInboundMedia({
196
- ...dmParams,
197
- attachments: [
198
- {
199
- contentType: "text/html",
200
- content: '<div>A file <attachment id="att-0"></attachment></div>',
201
- },
202
- ],
203
- });
204
-
205
- expect(downloadMSTeamsBotFrameworkAttachments).toHaveBeenCalledTimes(1);
206
- const call = firstBotFrameworkAttachmentCall();
207
- expect(call?.serviceUrl).toBe(dmParams.serviceUrl);
208
- expect(call?.attachmentIds).toEqual(["att-0", "att-1"]);
209
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
210
- expect(mediaList).toHaveLength(1);
211
- expect(mediaList[0].path).toBe("/tmp/report.pdf");
212
- });
213
-
214
- it("skips the Graph fallback entirely for 'a:' conversation IDs", async () => {
215
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
216
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockClear();
217
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockResolvedValue({
218
- media: [],
219
- attachmentCount: 1,
220
- });
221
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
222
- vi.mocked(buildMSTeamsGraphMessageUrls).mockClear();
223
-
224
- await resolveMSTeamsInboundMedia({
225
- ...dmParams,
226
- attachments: [
227
- {
228
- contentType: "text/html",
229
- content: '<div><attachment id="att-0"></attachment></div>',
230
- },
231
- ],
232
- });
233
-
234
- expect(downloadMSTeamsBotFrameworkAttachments).toHaveBeenCalled();
235
- expect(buildMSTeamsGraphMessageUrls).not.toHaveBeenCalled();
236
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
237
- });
238
-
239
- it("does NOT call the Bot Framework endpoint for Graph-compatible '19:' IDs", async () => {
240
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
241
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockClear();
242
- vi.mocked(downloadMSTeamsGraphMedia).mockResolvedValue({ media: [] });
243
-
244
- await resolveMSTeamsInboundMedia({
245
- ...baseParams,
246
- conversationId: "19:abc@thread.tacv2",
247
- serviceUrl: "https://smba.trafficmanager.net/amer/",
248
- attachments: [
249
- {
250
- contentType: "text/html",
251
- content: '<div><attachment id="att-0"></attachment></div>',
252
- },
253
- ],
254
- });
255
-
256
- expect(downloadMSTeamsBotFrameworkAttachments).not.toHaveBeenCalled();
257
- expect(downloadMSTeamsGraphMedia).toHaveBeenCalled();
258
- });
259
-
260
- it("skips BF DM attachment fetch entirely when HTML has no <attachment> tags", async () => {
261
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
262
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockClear();
263
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
264
- // Mention-only HTML (no `<attachment id="...">` tag) → extractor
265
- // returns []. The fallback skips both the Bot Framework and Graph
266
- // paths so we do not emit spurious 404 diagnostics (#58617).
267
- vi.mocked(extractMSTeamsHtmlAttachmentIds).mockReturnValueOnce([]);
268
-
269
- await resolveMSTeamsInboundMedia({
270
- ...dmParams,
271
- attachments: [
272
- {
273
- contentType: "text/html",
274
- content: '<div><at id="0">Bot</at> hello</div>',
275
- },
276
- ],
277
- });
278
-
279
- expect(downloadMSTeamsBotFrameworkAttachments).not.toHaveBeenCalled();
280
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
281
- });
282
-
283
- it("logs when serviceUrl is missing for a BF DM with HTML content", async () => {
284
- vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
285
- vi.mocked(downloadMSTeamsBotFrameworkAttachments).mockClear();
286
- vi.mocked(downloadMSTeamsGraphMedia).mockClear();
287
- vi.mocked(buildMSTeamsGraphMessageUrls).mockClear();
288
- const log = { debug: vi.fn() };
289
-
290
- await resolveMSTeamsInboundMedia({
291
- ...baseParams,
292
- log,
293
- conversationType: "personal",
294
- conversationId: "a:bf-dm-id",
295
- attachments: [
296
- {
297
- contentType: "text/html",
298
- content: '<div><attachment id="att-0"></attachment></div>',
299
- },
300
- ],
301
- });
302
-
303
- expect(downloadMSTeamsBotFrameworkAttachments).not.toHaveBeenCalled();
304
- // Graph fallback is also skipped because the ID is 'a:'
305
- expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
306
- expect(log.debug).toHaveBeenCalledWith(
307
- "bot framework attachment skipped (missing serviceUrl)",
308
- {
309
- conversationType: "personal",
310
- conversationId: "a:bf-dm-id",
311
- },
312
- );
313
- });
314
- });
@@ -1,180 +0,0 @@
1
- import {
2
- buildMSTeamsGraphMessageUrls,
3
- downloadMSTeamsAttachments,
4
- downloadMSTeamsBotFrameworkAttachments,
5
- downloadMSTeamsGraphMedia,
6
- extractMSTeamsHtmlAttachmentIds,
7
- isBotFrameworkPersonalChatId,
8
- type MSTeamsAccessTokenProvider,
9
- type MSTeamsAttachmentLike,
10
- type MSTeamsHtmlAttachmentSummary,
11
- type MSTeamsInboundMedia,
12
- } from "../attachments.js";
13
- import type { MSTeamsTurnContext } from "../sdk-types.js";
14
-
15
- type MSTeamsLogger = {
16
- debug?: (message: string, meta?: Record<string, unknown>) => void;
17
- warn?: (message: string, meta?: Record<string, unknown>) => void;
18
- error?: (message: string, meta?: Record<string, unknown>) => void;
19
- };
20
-
21
- export async function resolveMSTeamsInboundMedia(params: {
22
- attachments: MSTeamsAttachmentLike[];
23
- htmlSummary?: MSTeamsHtmlAttachmentSummary;
24
- maxBytes: number;
25
- allowHosts?: string[];
26
- authAllowHosts?: string[];
27
- tokenProvider: MSTeamsAccessTokenProvider;
28
- conversationType: string;
29
- conversationId: string;
30
- conversationMessageId?: string;
31
- serviceUrl?: string;
32
- activity: Pick<MSTeamsTurnContext["activity"], "id" | "replyToId" | "channelData">;
33
- log: MSTeamsLogger;
34
- /** When true, embeds original filename in stored path for later extraction. */
35
- preserveFilenames?: boolean;
36
- }): Promise<MSTeamsInboundMedia[]> {
37
- const {
38
- attachments,
39
- htmlSummary,
40
- maxBytes,
41
- tokenProvider,
42
- allowHosts,
43
- conversationType,
44
- conversationId,
45
- conversationMessageId,
46
- serviceUrl,
47
- activity,
48
- log,
49
- preserveFilenames,
50
- } = params;
51
-
52
- let mediaList = await downloadMSTeamsAttachments({
53
- attachments,
54
- maxBytes,
55
- tokenProvider,
56
- allowHosts,
57
- authAllowHosts: params.authAllowHosts,
58
- preserveFilenames,
59
- logger: log,
60
- });
61
-
62
- if (mediaList.length === 0) {
63
- // Gate the Graph/Bot Framework media fallback on the presence of real
64
- // `<attachment id="...">` tags inside any `text/html` attachment. Teams
65
- // delivers @mention cards and other chrome as `text/html` attachments
66
- // too, so keying off contentType alone produces spurious 404 diagnostics
67
- // for every mention-only message and masks real file attachments (#58617).
68
- const attachmentIds = extractMSTeamsHtmlAttachmentIds(attachments);
69
- const hasHtmlFileAttachment = attachmentIds.length > 0;
70
-
71
- // Personal DMs with the bot use Bot Framework conversation IDs (`a:...`
72
- // or `8:orgid:...`) which Graph's `/chats/{id}` endpoint rejects with
73
- // "Invalid ThreadId". Fetch media via the Bot Framework v3 attachments
74
- // endpoint instead, which speaks the same identifier space.
75
- if (hasHtmlFileAttachment && isBotFrameworkPersonalChatId(conversationId)) {
76
- if (!serviceUrl) {
77
- log.debug?.("bot framework attachment skipped (missing serviceUrl)", {
78
- conversationType,
79
- conversationId,
80
- });
81
- } else {
82
- const bfMedia = await downloadMSTeamsBotFrameworkAttachments({
83
- serviceUrl,
84
- attachmentIds,
85
- tokenProvider,
86
- maxBytes,
87
- allowHosts,
88
- authAllowHosts: params.authAllowHosts,
89
- preserveFilenames,
90
- });
91
- if (bfMedia.media.length > 0) {
92
- mediaList = bfMedia.media;
93
- } else {
94
- log.debug?.("bot framework attachments fetch empty", {
95
- conversationType,
96
- attachmentCount: bfMedia.attachmentCount ?? attachmentIds.length,
97
- });
98
- }
99
- }
100
- }
101
-
102
- if (
103
- hasHtmlFileAttachment &&
104
- mediaList.length === 0 &&
105
- !isBotFrameworkPersonalChatId(conversationId)
106
- ) {
107
- const messageUrls = buildMSTeamsGraphMessageUrls({
108
- conversationType,
109
- conversationId,
110
- messageId: activity.id ?? undefined,
111
- replyToId: activity.replyToId ?? undefined,
112
- conversationMessageId,
113
- channelData: activity.channelData,
114
- });
115
- if (messageUrls.length === 0) {
116
- log.debug?.("graph message url unavailable", {
117
- conversationType,
118
- hasChannelData: Boolean(activity.channelData),
119
- messageId: activity.id ?? undefined,
120
- replyToId: activity.replyToId ?? undefined,
121
- });
122
- } else {
123
- const attempts: Array<{
124
- url: string;
125
- hostedStatus?: number;
126
- attachmentStatus?: number;
127
- hostedCount?: number;
128
- attachmentCount?: number;
129
- tokenError?: boolean;
130
- }> = [];
131
- for (const messageUrl of messageUrls) {
132
- const graphMedia = await downloadMSTeamsGraphMedia({
133
- messageUrl,
134
- tokenProvider,
135
- maxBytes,
136
- allowHosts,
137
- authAllowHosts: params.authAllowHosts,
138
- preserveFilenames,
139
- log,
140
- logger: log,
141
- });
142
- attempts.push({
143
- url: messageUrl,
144
- hostedStatus: graphMedia.hostedStatus,
145
- attachmentStatus: graphMedia.attachmentStatus,
146
- hostedCount: graphMedia.hostedCount,
147
- attachmentCount: graphMedia.attachmentCount,
148
- tokenError: graphMedia.tokenError,
149
- });
150
- if (graphMedia.media.length > 0) {
151
- mediaList = graphMedia.media;
152
- break;
153
- }
154
- if (graphMedia.tokenError) {
155
- break;
156
- }
157
- }
158
- if (mediaList.length === 0) {
159
- log.debug?.("graph media fetch empty", {
160
- attempts,
161
- attachmentIdCount: attachmentIds.length,
162
- });
163
- }
164
- }
165
- }
166
- }
167
-
168
- if (mediaList.length > 0) {
169
- log.debug?.("downloaded attachments", { count: mediaList.length });
170
- } else if (htmlSummary?.imgTags) {
171
- log.debug?.("inline images detected but none downloaded", {
172
- imgTags: htmlSummary.imgTags,
173
- srcHosts: htmlSummary.srcHosts,
174
- dataImages: htmlSummary.dataImages,
175
- cidImages: htmlSummary.cidImages,
176
- });
177
- }
178
-
179
- return mediaList;
180
- }
@@ -1,28 +0,0 @@
1
- import { vi } from "vitest";
2
-
3
- const runtimeApiMockState = vi.hoisted(() => ({
4
- dispatchReplyFromConfigWithSettledDispatcher: vi.fn(async (params: { ctxPayload: unknown }) => ({
5
- queuedFinal: false,
6
- counts: {},
7
- capturedCtxPayload: params.ctxPayload,
8
- })),
9
- }));
10
-
11
- export function getRuntimeApiMockState() {
12
- return runtimeApiMockState;
13
- }
14
-
15
- vi.mock("klaw/plugin-sdk/inbound-reply-dispatch", () => {
16
- return {
17
- dispatchReplyFromConfigWithSettledDispatcher:
18
- runtimeApiMockState.dispatchReplyFromConfigWithSettledDispatcher,
19
- };
20
- });
21
-
22
- vi.mock("../reply-dispatcher.js", () => ({
23
- createMSTeamsReplyDispatcher: () => ({
24
- dispatcher: {},
25
- replyOptions: {},
26
- markDispatchIdle: vi.fn(),
27
- }),
28
- }));