@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,328 +0,0 @@
1
- import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
2
- import { prepareFileConsentActivity, requiresFileConsent } from "./file-consent-helpers.js";
3
- import {
4
- clearPendingUploads,
5
- getPendingUpload,
6
- getPendingUploadCount,
7
- removePendingUpload,
8
- storePendingUpload,
9
- } from "./pending-uploads.js";
10
- import * as pendingUploads from "./pending-uploads.js";
11
-
12
- describe("requiresFileConsent", () => {
13
- const thresholdBytes = 4 * 1024 * 1024; // 4MB
14
-
15
- it("returns true for personal chat with non-image", () => {
16
- expect(
17
- requiresFileConsent({
18
- conversationType: "personal",
19
- contentType: "application/pdf",
20
- bufferSize: 1000,
21
- thresholdBytes,
22
- }),
23
- ).toBe(true);
24
- });
25
-
26
- it("returns true for personal chat with large image", () => {
27
- expect(
28
- requiresFileConsent({
29
- conversationType: "personal",
30
- contentType: "image/png",
31
- bufferSize: 5 * 1024 * 1024, // 5MB
32
- thresholdBytes,
33
- }),
34
- ).toBe(true);
35
- });
36
-
37
- it("returns false for personal chat with small image", () => {
38
- expect(
39
- requiresFileConsent({
40
- conversationType: "personal",
41
- contentType: "image/png",
42
- bufferSize: 1000,
43
- thresholdBytes,
44
- }),
45
- ).toBe(false);
46
- });
47
-
48
- it("returns false for group chat with large non-image", () => {
49
- expect(
50
- requiresFileConsent({
51
- conversationType: "groupChat",
52
- contentType: "application/pdf",
53
- bufferSize: 5 * 1024 * 1024,
54
- thresholdBytes,
55
- }),
56
- ).toBe(false);
57
- });
58
-
59
- it("returns false for channel with large non-image", () => {
60
- expect(
61
- requiresFileConsent({
62
- conversationType: "channel",
63
- contentType: "application/pdf",
64
- bufferSize: 5 * 1024 * 1024,
65
- thresholdBytes,
66
- }),
67
- ).toBe(false);
68
- });
69
-
70
- it("handles case-insensitive conversation type", () => {
71
- expect(
72
- requiresFileConsent({
73
- conversationType: "Personal",
74
- contentType: "application/pdf",
75
- bufferSize: 1000,
76
- thresholdBytes,
77
- }),
78
- ).toBe(true);
79
-
80
- expect(
81
- requiresFileConsent({
82
- conversationType: "PERSONAL",
83
- contentType: "application/pdf",
84
- bufferSize: 1000,
85
- thresholdBytes,
86
- }),
87
- ).toBe(true);
88
- });
89
-
90
- it("returns false when conversationType is undefined", () => {
91
- expect(
92
- requiresFileConsent({
93
- conversationType: undefined,
94
- contentType: "application/pdf",
95
- bufferSize: 1000,
96
- thresholdBytes,
97
- }),
98
- ).toBe(false);
99
- });
100
-
101
- it("returns true for personal chat when contentType is undefined (non-image)", () => {
102
- expect(
103
- requiresFileConsent({
104
- conversationType: "personal",
105
- contentType: undefined,
106
- bufferSize: 1000,
107
- thresholdBytes,
108
- }),
109
- ).toBe(true);
110
- });
111
-
112
- it("returns true for personal chat with file exactly at threshold", () => {
113
- expect(
114
- requiresFileConsent({
115
- conversationType: "personal",
116
- contentType: "image/jpeg",
117
- bufferSize: thresholdBytes, // exactly 4MB
118
- thresholdBytes,
119
- }),
120
- ).toBe(true);
121
- });
122
-
123
- it("returns false for personal chat with file just below threshold", () => {
124
- expect(
125
- requiresFileConsent({
126
- conversationType: "personal",
127
- contentType: "image/jpeg",
128
- bufferSize: thresholdBytes - 1, // 4MB - 1 byte
129
- thresholdBytes,
130
- }),
131
- ).toBe(false);
132
- });
133
- });
134
-
135
- describe("prepareFileConsentActivity", () => {
136
- const mockUploadId = "test-upload-id-123";
137
-
138
- beforeEach(() => {
139
- vi.spyOn(pendingUploads, "storePendingUpload").mockReturnValue(mockUploadId);
140
- });
141
-
142
- afterEach(() => {
143
- vi.restoreAllMocks();
144
- });
145
-
146
- it("creates activity with consent card attachment", () => {
147
- const result = prepareFileConsentActivity({
148
- media: {
149
- buffer: Buffer.from("test content"),
150
- filename: "test.pdf",
151
- contentType: "application/pdf",
152
- },
153
- conversationId: "conv123",
154
- description: "My file",
155
- });
156
-
157
- expect(result.uploadId).toBe(mockUploadId);
158
- expect(result.activity.type).toBe("message");
159
- expect(result.activity.attachments).toHaveLength(1);
160
-
161
- const attachment = (result.activity.attachments as unknown[])[0] as Record<string, unknown>;
162
- expect(attachment.contentType).toBe("application/vnd.microsoft.teams.card.file.consent");
163
- expect(attachment.name).toBe("test.pdf");
164
- });
165
-
166
- it("stores pending upload with correct data", () => {
167
- const buffer = Buffer.from("test content");
168
- prepareFileConsentActivity({
169
- media: {
170
- buffer,
171
- filename: "test.pdf",
172
- contentType: "application/pdf",
173
- },
174
- conversationId: "conv123",
175
- description: "My file",
176
- });
177
-
178
- expect(pendingUploads.storePendingUpload).toHaveBeenCalledWith({
179
- buffer,
180
- filename: "test.pdf",
181
- contentType: "application/pdf",
182
- conversationId: "conv123",
183
- });
184
- });
185
-
186
- it("uses default description when not provided", () => {
187
- const result = prepareFileConsentActivity({
188
- media: {
189
- buffer: Buffer.from("test"),
190
- filename: "document.docx",
191
- contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
192
- },
193
- conversationId: "conv456",
194
- });
195
-
196
- const attachment = (result.activity.attachments as unknown[])[0] as Record<
197
- string,
198
- { description: string }
199
- >;
200
- expect(attachment.content.description).toBe("File: document.docx");
201
- });
202
-
203
- it("uses provided description", () => {
204
- const result = prepareFileConsentActivity({
205
- media: {
206
- buffer: Buffer.from("test"),
207
- filename: "report.pdf",
208
- contentType: "application/pdf",
209
- },
210
- conversationId: "conv789",
211
- description: "Q4 Financial Report",
212
- });
213
-
214
- const attachment = (result.activity.attachments as unknown[])[0] as Record<
215
- string,
216
- { description: string }
217
- >;
218
- expect(attachment.content.description).toBe("Q4 Financial Report");
219
- });
220
-
221
- it("includes uploadId in consent card context", () => {
222
- const result = prepareFileConsentActivity({
223
- media: {
224
- buffer: Buffer.from("test"),
225
- filename: "file.txt",
226
- contentType: "text/plain",
227
- },
228
- conversationId: "conv000",
229
- });
230
-
231
- const attachment = (result.activity.attachments as unknown[])[0] as Record<
232
- string,
233
- { acceptContext: { uploadId: string } }
234
- >;
235
- expect(attachment.content.acceptContext.uploadId).toBe(mockUploadId);
236
- });
237
-
238
- it("handles media without contentType", () => {
239
- const result = prepareFileConsentActivity({
240
- media: {
241
- buffer: Buffer.from("binary data"),
242
- filename: "unknown.bin",
243
- },
244
- conversationId: "conv111",
245
- });
246
-
247
- expect(result.uploadId).toBe(mockUploadId);
248
- expect(result.activity.type).toBe("message");
249
- });
250
- });
251
-
252
- describe("msteams pending uploads", () => {
253
- beforeEach(() => {
254
- vi.useFakeTimers();
255
- clearPendingUploads();
256
- });
257
-
258
- afterEach(() => {
259
- clearPendingUploads();
260
- vi.useRealTimers();
261
- });
262
-
263
- it("stores uploads, exposes them by id, and tracks count", () => {
264
- const id = storePendingUpload({
265
- buffer: Buffer.from("hello"),
266
- filename: "hello.txt",
267
- contentType: "text/plain",
268
- conversationId: "conv-1",
269
- });
270
-
271
- expect(getPendingUploadCount()).toBe(1);
272
- const pendingUpload = getPendingUpload(id);
273
- expect(pendingUpload).toEqual({
274
- id,
275
- buffer: Buffer.from("hello"),
276
- filename: "hello.txt",
277
- contentType: "text/plain",
278
- conversationId: "conv-1",
279
- createdAt: pendingUpload?.createdAt,
280
- });
281
- expect(typeof pendingUpload?.createdAt).toBe("number");
282
- });
283
-
284
- it("removes uploads explicitly and ignores empty ids", () => {
285
- const id = storePendingUpload({
286
- buffer: Buffer.from("hello"),
287
- filename: "hello.txt",
288
- conversationId: "conv-1",
289
- });
290
-
291
- removePendingUpload(undefined);
292
- expect(getPendingUploadCount()).toBe(1);
293
-
294
- removePendingUpload(id);
295
- expect(getPendingUpload(id)).toBeUndefined();
296
- expect(getPendingUploadCount()).toBe(0);
297
- });
298
-
299
- it("expires uploads by ttl even if the timeout callback has not been observed yet", () => {
300
- const id = storePendingUpload({
301
- buffer: Buffer.from("hello"),
302
- filename: "hello.txt",
303
- conversationId: "conv-1",
304
- });
305
-
306
- vi.advanceTimersByTime(5 * 60 * 1000 + 1);
307
-
308
- expect(getPendingUpload(id)).toBeUndefined();
309
- expect(getPendingUploadCount()).toBe(0);
310
- });
311
-
312
- it("clears all uploads for test cleanup", () => {
313
- storePendingUpload({
314
- buffer: Buffer.from("a"),
315
- filename: "a.txt",
316
- conversationId: "conv-1",
317
- });
318
- storePendingUpload({
319
- buffer: Buffer.from("b"),
320
- filename: "b.txt",
321
- conversationId: "conv-2",
322
- });
323
-
324
- clearPendingUploads();
325
-
326
- expect(getPendingUploadCount()).toBe(0);
327
- });
328
- });
@@ -1,115 +0,0 @@
1
- import { normalizeOptionalLowercaseString } from "klaw/plugin-sdk/string-coerce-runtime";
2
- import { buildFileConsentCard } from "./file-consent.js";
3
- import { storePendingUploadFs } from "./pending-uploads-fs.js";
4
- import { storePendingUpload } from "./pending-uploads.js";
5
-
6
- type FileConsentMedia = {
7
- buffer: Buffer;
8
- filename: string;
9
- contentType?: string;
10
- };
11
-
12
- type FileConsentActivityResult = {
13
- activity: Record<string, unknown>;
14
- uploadId: string;
15
- };
16
-
17
- function buildConsentActivity(params: {
18
- media: FileConsentMedia;
19
- description?: string;
20
- uploadId: string;
21
- }): Record<string, unknown> {
22
- const { media, description, uploadId } = params;
23
- const consentCard = buildFileConsentCard({
24
- filename: media.filename,
25
- description: description || `File: ${media.filename}`,
26
- sizeInBytes: media.buffer.length,
27
- context: { uploadId },
28
- });
29
- return {
30
- type: "message",
31
- attachments: [consentCard],
32
- };
33
- }
34
-
35
- /**
36
- * Prepare a FileConsentCard activity for large files or non-images in personal chats.
37
- * Returns the activity object and uploadId - caller is responsible for sending.
38
- *
39
- * This variant only writes to the in-memory store. Use it when the caller and
40
- * the `fileConsent/invoke` handler share the same process (for example the
41
- * messenger reply path). For proactive CLI sends where the invoke arrives in
42
- * a different process, use {@link prepareFileConsentActivityFs} instead.
43
- */
44
- export function prepareFileConsentActivity(params: {
45
- media: FileConsentMedia;
46
- conversationId: string;
47
- description?: string;
48
- }): FileConsentActivityResult {
49
- const { media, conversationId, description } = params;
50
-
51
- const uploadId = storePendingUpload({
52
- buffer: media.buffer,
53
- filename: media.filename,
54
- contentType: media.contentType,
55
- conversationId,
56
- });
57
-
58
- const activity = buildConsentActivity({ media, description, uploadId });
59
- return { activity, uploadId };
60
- }
61
-
62
- /**
63
- * Prepare a FileConsentCard activity and persist the pending upload to the
64
- * filesystem so a different process can read it when the user accepts.
65
- *
66
- * This is used by the proactive CLI `message send --media` path: the CLI
67
- * process sends the card and exits, but the `fileConsent/invoke` callback is
68
- * delivered to the long-lived gateway monitor process. The FS-backed store
69
- * bridges those two processes. The in-memory store is also populated so
70
- * same-process flows keep the fast path.
71
- */
72
- export async function prepareFileConsentActivityFs(params: {
73
- media: FileConsentMedia;
74
- conversationId: string;
75
- description?: string;
76
- }): Promise<FileConsentActivityResult> {
77
- const { media, conversationId, description } = params;
78
-
79
- // Populate the in-memory store first so the uploadId is consistent, then
80
- // mirror the same entry to the FS store under the same id so an invoke
81
- // handler in another process can find it.
82
- const uploadId = storePendingUpload({
83
- buffer: media.buffer,
84
- filename: media.filename,
85
- contentType: media.contentType,
86
- conversationId,
87
- });
88
-
89
- await storePendingUploadFs({
90
- id: uploadId,
91
- buffer: media.buffer,
92
- filename: media.filename,
93
- contentType: media.contentType,
94
- conversationId,
95
- });
96
-
97
- const activity = buildConsentActivity({ media, description, uploadId });
98
- return { activity, uploadId };
99
- }
100
-
101
- /**
102
- * Check if a file requires FileConsentCard flow.
103
- * True for: personal chat AND (large file OR non-image)
104
- */
105
- export function requiresFileConsent(params: {
106
- conversationType: string | undefined;
107
- contentType: string | undefined;
108
- bufferSize: number;
109
- thresholdBytes: number;
110
- }): boolean {
111
- const isPersonal = normalizeOptionalLowercaseString(params.conversationType) === "personal";
112
- const isImage = params.contentType?.startsWith("image/") ?? false;
113
- const isLargeFile = params.bufferSize >= params.thresholdBytes;
114
- return isPersonal && (isLargeFile || !isImage);
115
- }
@@ -1,150 +0,0 @@
1
- import { formatUnknownError } from "./errors.js";
2
- import { buildFileInfoCard, parseFileConsentInvoke, uploadToConsentUrl } from "./file-consent.js";
3
- import { normalizeMSTeamsConversationId } from "./inbound.js";
4
- import type { MSTeamsMonitorLogger } from "./monitor-types.js";
5
- import { getPendingUploadFs, removePendingUploadFs } from "./pending-uploads-fs.js";
6
- import { getPendingUpload, removePendingUpload } from "./pending-uploads.js";
7
- import { withRevokedProxyFallback } from "./revoked-context.js";
8
- import type { MSTeamsTurnContext } from "./sdk-types.js";
9
-
10
- /**
11
- * Handle fileConsent/invoke activities for large file uploads.
12
- */
13
- async function handleMSTeamsFileConsentInvoke(
14
- context: MSTeamsTurnContext,
15
- log: MSTeamsMonitorLogger,
16
- ): Promise<boolean> {
17
- const expiredUploadMessage =
18
- "The file upload request has expired. Please try sending the file again.";
19
- const activity = context.activity;
20
- if (activity.type !== "invoke" || activity.name !== "fileConsent/invoke") {
21
- return false;
22
- }
23
-
24
- const consentResponse = parseFileConsentInvoke(activity);
25
- if (!consentResponse) {
26
- log.debug?.("invalid file consent invoke", { value: activity.value });
27
- return false;
28
- }
29
-
30
- const uploadId =
31
- typeof consentResponse.context?.uploadId === "string"
32
- ? consentResponse.context.uploadId
33
- : undefined;
34
- // Prefer the in-memory store (same-process reply path); fall back to the
35
- // FS-backed store so CLI `message send --media` flows work even when the
36
- // invoke callback is delivered to a different process.
37
- const inMemoryFile = getPendingUpload(uploadId);
38
- const fsFile = inMemoryFile ? undefined : await getPendingUploadFs(uploadId);
39
- const pendingFile:
40
- | {
41
- buffer: Buffer;
42
- filename: string;
43
- contentType?: string;
44
- conversationId: string;
45
- consentCardActivityId?: string;
46
- }
47
- | undefined = inMemoryFile ?? fsFile;
48
- if (pendingFile) {
49
- const pendingConversationId = normalizeMSTeamsConversationId(pendingFile.conversationId);
50
- const invokeConversationId = normalizeMSTeamsConversationId(activity.conversation?.id ?? "");
51
- if (!invokeConversationId || pendingConversationId !== invokeConversationId) {
52
- log.info("file consent conversation mismatch", {
53
- uploadId,
54
- expectedConversationId: pendingConversationId,
55
- receivedConversationId: invokeConversationId || undefined,
56
- });
57
- if (consentResponse.action === "accept") {
58
- await context.sendActivity(expiredUploadMessage);
59
- }
60
- return true;
61
- }
62
- }
63
-
64
- if (consentResponse.action === "accept" && consentResponse.uploadInfo) {
65
- if (pendingFile) {
66
- log.debug?.("user accepted file consent, uploading", {
67
- uploadId,
68
- filename: pendingFile.filename,
69
- size: pendingFile.buffer.length,
70
- });
71
-
72
- try {
73
- await uploadToConsentUrl({
74
- url: consentResponse.uploadInfo.uploadUrl,
75
- buffer: pendingFile.buffer,
76
- contentType: pendingFile.contentType,
77
- });
78
-
79
- const fileInfoCard = buildFileInfoCard({
80
- filename: consentResponse.uploadInfo.name,
81
- contentUrl: consentResponse.uploadInfo.contentUrl,
82
- uniqueId: consentResponse.uploadInfo.uniqueId,
83
- fileType: consentResponse.uploadInfo.fileType,
84
- });
85
-
86
- if (!pendingFile.consentCardActivityId) {
87
- await context.sendActivity({
88
- type: "message",
89
- attachments: [fileInfoCard],
90
- });
91
- }
92
-
93
- if (pendingFile.consentCardActivityId) {
94
- try {
95
- await context.updateActivity({
96
- id: pendingFile.consentCardActivityId,
97
- type: "message",
98
- attachments: [fileInfoCard],
99
- });
100
- } catch {
101
- await context.sendActivity({
102
- type: "message",
103
- attachments: [fileInfoCard],
104
- });
105
- }
106
- }
107
-
108
- log.info("file upload complete", {
109
- uploadId,
110
- filename: consentResponse.uploadInfo.name,
111
- uniqueId: consentResponse.uploadInfo.uniqueId,
112
- });
113
- } catch (err) {
114
- log.error("file upload failed", { uploadId, error: formatUnknownError(err) });
115
- await context.sendActivity("File upload failed. Please try again.");
116
- } finally {
117
- removePendingUpload(uploadId);
118
- await removePendingUploadFs(uploadId);
119
- }
120
- } else {
121
- log.debug?.("pending file not found for consent", { uploadId });
122
- await context.sendActivity(expiredUploadMessage);
123
- }
124
- } else {
125
- log.debug?.("user declined file consent", { uploadId });
126
- removePendingUpload(uploadId);
127
- await removePendingUploadFs(uploadId);
128
- }
129
-
130
- return true;
131
- }
132
-
133
- export async function respondToMSTeamsFileConsentInvoke(
134
- context: MSTeamsTurnContext,
135
- log: MSTeamsMonitorLogger,
136
- ): Promise<void> {
137
- await context.sendActivity({ type: "invokeResponse", value: { status: 200 } });
138
-
139
- try {
140
- await withRevokedProxyFallback({
141
- run: async () => await handleMSTeamsFileConsentInvoke(context, log),
142
- onRevoked: async () => true,
143
- onRevokedLog: () => {
144
- log.debug?.("turn context revoked during file consent invoke; skipping delayed response");
145
- },
146
- });
147
- } catch (err) {
148
- log.debug?.("file consent handler error", { error: formatUnknownError(err) });
149
- }
150
- }