@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
package/src/send.ts DELETED
@@ -1,637 +0,0 @@
1
- import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime";
2
- import { convertMarkdownTables } from "openclaw/plugin-sdk/text-runtime";
3
- import { loadOutboundMediaFromUrl, type OpenClawConfig } from "../runtime-api.js";
4
- import {
5
- classifyMSTeamsSendError,
6
- formatMSTeamsSendErrorHint,
7
- formatUnknownError,
8
- } from "./errors.js";
9
- import { prepareFileConsentActivityFs, requiresFileConsent } from "./file-consent-helpers.js";
10
- import { buildTeamsFileInfoCard } from "./graph-chat.js";
11
- import {
12
- getDriveItemProperties,
13
- uploadAndShareOneDrive,
14
- uploadAndShareSharePoint,
15
- } from "./graph-upload.js";
16
- import { extractFilename, extractMessageId } from "./media-helpers.js";
17
- import { buildConversationReference, sendMSTeamsMessages } from "./messenger.js";
18
- import { setPendingUploadActivityIdFs } from "./pending-uploads-fs.js";
19
- import { setPendingUploadActivityId } from "./pending-uploads.js";
20
- import { buildMSTeamsPollCard } from "./polls.js";
21
- import { resolveMSTeamsSendContext, type MSTeamsProactiveContext } from "./send-context.js";
22
-
23
- type SendMSTeamsMessageParams = {
24
- /** Full config (for credentials) */
25
- cfg: OpenClawConfig;
26
- /** Conversation ID or user ID to send to */
27
- to: string;
28
- /** Message text */
29
- text: string;
30
- /** Optional media URL */
31
- mediaUrl?: string;
32
- /** Optional filename override for uploaded media/files */
33
- filename?: string;
34
- mediaLocalRoots?: readonly string[];
35
- mediaReadFile?: (filePath: string) => Promise<Buffer>;
36
- };
37
-
38
- type SendMSTeamsMessageResult = {
39
- messageId: string;
40
- conversationId: string;
41
- /** If a FileConsentCard was sent instead of the file, this contains the upload ID */
42
- pendingUploadId?: string;
43
- };
44
-
45
- /** Threshold for large files that require FileConsentCard flow in personal chats */
46
- const FILE_CONSENT_THRESHOLD_BYTES = 4 * 1024 * 1024; // 4MB
47
-
48
- /**
49
- * MSTeams-specific media size limit (100MB).
50
- * Higher than the default because OneDrive upload handles large files well.
51
- */
52
- const MSTEAMS_MAX_MEDIA_BYTES = 100 * 1024 * 1024;
53
-
54
- type SendMSTeamsPollParams = {
55
- /** Full config (for credentials) */
56
- cfg: OpenClawConfig;
57
- /** Conversation ID or user ID to send to */
58
- to: string;
59
- /** Poll question */
60
- question: string;
61
- /** Poll options */
62
- options: string[];
63
- /** Max selections (defaults to 1) */
64
- maxSelections?: number;
65
- };
66
-
67
- type SendMSTeamsPollResult = {
68
- pollId: string;
69
- messageId: string;
70
- conversationId: string;
71
- };
72
-
73
- type SendMSTeamsCardParams = {
74
- /** Full config (for credentials) */
75
- cfg: OpenClawConfig;
76
- /** Conversation ID or user ID to send to */
77
- to: string;
78
- /** Adaptive Card JSON object */
79
- card: Record<string, unknown>;
80
- };
81
-
82
- type SendMSTeamsCardResult = {
83
- messageId: string;
84
- conversationId: string;
85
- };
86
-
87
- /**
88
- * Send a message to a Teams conversation or user.
89
- *
90
- * Uses the stored ConversationReference from previous interactions.
91
- * The bot must have received at least one message from the conversation
92
- * before proactive messaging works.
93
- *
94
- * File handling by conversation type:
95
- * - Personal (1:1) chats: small images (<4MB) use base64, large files and non-images use FileConsentCard
96
- * - Group chats / channels: files are uploaded to OneDrive and shared via link
97
- */
98
- export async function sendMessageMSTeams(
99
- params: SendMSTeamsMessageParams,
100
- ): Promise<SendMSTeamsMessageResult> {
101
- const { cfg, to, text, mediaUrl, filename, mediaLocalRoots, mediaReadFile } = params;
102
- const tableMode = resolveMarkdownTableMode({
103
- cfg,
104
- channel: "msteams",
105
- });
106
- const messageText = convertMarkdownTables(text ?? "", tableMode);
107
- const ctx = await resolveMSTeamsSendContext({ cfg, to });
108
- const {
109
- adapter,
110
- appId,
111
- conversationId,
112
- ref,
113
- log,
114
- conversationType,
115
- tokenProvider,
116
- sharePointSiteId,
117
- } = ctx;
118
-
119
- log.debug?.("sending proactive message", {
120
- conversationId,
121
- conversationType,
122
- textLength: messageText.length,
123
- hasMedia: Boolean(mediaUrl),
124
- });
125
-
126
- // Handle media if present
127
- if (mediaUrl) {
128
- const mediaMaxBytes = ctx.mediaMaxBytes ?? MSTEAMS_MAX_MEDIA_BYTES;
129
- const media = await loadOutboundMediaFromUrl(mediaUrl, {
130
- maxBytes: mediaMaxBytes,
131
- mediaLocalRoots,
132
- mediaReadFile,
133
- });
134
- const isLargeFile = media.buffer.length >= FILE_CONSENT_THRESHOLD_BYTES;
135
- const isImage = media.contentType?.startsWith("image/") ?? false;
136
- const fallbackFileName = await extractFilename(mediaUrl);
137
- const fileName = filename?.trim() || media.fileName || fallbackFileName;
138
-
139
- log.debug?.("processing media", {
140
- fileName,
141
- contentType: media.contentType,
142
- size: media.buffer.length,
143
- isLargeFile,
144
- isImage,
145
- conversationType,
146
- });
147
-
148
- // Personal chats: base64 only works for images; use FileConsentCard for large files or non-images
149
- if (
150
- requiresFileConsent({
151
- conversationType,
152
- contentType: media.contentType,
153
- bufferSize: media.buffer.length,
154
- thresholdBytes: FILE_CONSENT_THRESHOLD_BYTES,
155
- })
156
- ) {
157
- // Proactive CLI sends run in a different process from the gateway's
158
- // monitor that receives the fileConsent/invoke callback. Use the FS-
159
- // backed helper so the invoke handler can find the pending upload when
160
- // the user clicks "Allow".
161
- const { activity, uploadId } = await prepareFileConsentActivityFs({
162
- media: { buffer: media.buffer, filename: fileName, contentType: media.contentType },
163
- conversationId,
164
- description: messageText || undefined,
165
- });
166
-
167
- log.debug?.("sending file consent card", { uploadId, fileName, size: media.buffer.length });
168
-
169
- const messageId = await sendProactiveActivity({
170
- adapter,
171
- appId,
172
- ref,
173
- activity,
174
- errorPrefix: "msteams consent card send",
175
- });
176
-
177
- // Store the activity ID so the accept handler can replace the consent
178
- // card in-place. Mirror it into the FS store too because the invoke
179
- // callback may be delivered to a different process than the CLI send.
180
- setPendingUploadActivityId(uploadId, messageId);
181
- await setPendingUploadActivityIdFs(uploadId, messageId);
182
-
183
- log.info("sent file consent card", { conversationId, messageId, uploadId });
184
-
185
- return {
186
- messageId,
187
- conversationId,
188
- pendingUploadId: uploadId,
189
- };
190
- }
191
-
192
- // Personal chat with small image: use base64 (only works for images)
193
- if (conversationType === "personal") {
194
- // Small image in personal chat: use base64 (only works for images)
195
- const base64 = media.buffer.toString("base64");
196
- const finalMediaUrl = `data:${media.contentType};base64,${base64}`;
197
-
198
- return sendTextWithMedia(ctx, messageText, finalMediaUrl);
199
- }
200
-
201
- if (isImage && !sharePointSiteId) {
202
- // Group chat/channel without SharePoint: send image inline (avoids OneDrive failures)
203
- const base64 = media.buffer.toString("base64");
204
- const finalMediaUrl = `data:${media.contentType};base64,${base64}`;
205
- return sendTextWithMedia(ctx, messageText, finalMediaUrl);
206
- }
207
-
208
- // Group chat or channel: upload to SharePoint (if siteId configured) or OneDrive
209
- try {
210
- if (sharePointSiteId) {
211
- // Use SharePoint upload + Graph API for native file card
212
- log.debug?.("uploading to SharePoint for native file card", {
213
- fileName,
214
- conversationType,
215
- siteId: sharePointSiteId,
216
- });
217
-
218
- const uploaded = await uploadAndShareSharePoint({
219
- buffer: media.buffer,
220
- filename: fileName,
221
- contentType: media.contentType,
222
- tokenProvider,
223
- siteId: sharePointSiteId,
224
- // Use the Graph-native chat ID (19:xxx format) — the Bot Framework conversationId
225
- // for personal DMs uses a different format that Graph API rejects.
226
- chatId: ctx.graphChatId ?? conversationId,
227
- usePerUserSharing: conversationType === "groupChat",
228
- });
229
-
230
- log.debug?.("SharePoint upload complete", {
231
- itemId: uploaded.itemId,
232
- shareUrl: uploaded.shareUrl,
233
- });
234
-
235
- // Get driveItem properties needed for native file card
236
- const driveItem = await getDriveItemProperties({
237
- siteId: sharePointSiteId,
238
- itemId: uploaded.itemId,
239
- tokenProvider,
240
- });
241
-
242
- log.debug?.("driveItem properties retrieved", {
243
- eTag: driveItem.eTag,
244
- webDavUrl: driveItem.webDavUrl,
245
- });
246
-
247
- // Build native Teams file card attachment and send via Bot Framework
248
- const fileCardAttachment = buildTeamsFileInfoCard(driveItem);
249
- const activity = {
250
- type: "message",
251
- text: messageText || undefined,
252
- attachments: [fileCardAttachment],
253
- };
254
- const messageId = await sendProactiveActivityRaw({
255
- adapter,
256
- appId,
257
- ref,
258
- activity,
259
- });
260
-
261
- log.info("sent native file card", {
262
- conversationId,
263
- messageId,
264
- fileName: driveItem.name,
265
- });
266
-
267
- return { messageId, conversationId };
268
- }
269
-
270
- // Fallback: no SharePoint site configured, use OneDrive with markdown link
271
- log.debug?.("uploading to OneDrive (no SharePoint site configured)", {
272
- fileName,
273
- conversationType,
274
- });
275
-
276
- const uploaded = await uploadAndShareOneDrive({
277
- buffer: media.buffer,
278
- filename: fileName,
279
- contentType: media.contentType,
280
- tokenProvider,
281
- });
282
-
283
- log.debug?.("OneDrive upload complete", {
284
- itemId: uploaded.itemId,
285
- shareUrl: uploaded.shareUrl,
286
- });
287
-
288
- // Send message with file link (Bot Framework doesn't support "reference" attachment type for sending)
289
- const fileLink = `📎 [${uploaded.name}](${uploaded.shareUrl})`;
290
- const activity = {
291
- type: "message",
292
- text: messageText ? `${messageText}\n\n${fileLink}` : fileLink,
293
- };
294
- const messageId = await sendProactiveActivityRaw({
295
- adapter,
296
- appId,
297
- ref,
298
- activity,
299
- });
300
-
301
- log.info("sent message with OneDrive file link", {
302
- conversationId,
303
- messageId,
304
- shareUrl: uploaded.shareUrl,
305
- });
306
-
307
- return { messageId, conversationId };
308
- } catch (err) {
309
- const classification = classifyMSTeamsSendError(err);
310
- const hint = formatMSTeamsSendErrorHint(classification);
311
- const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
312
- throw new Error(
313
- `msteams file send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
314
- { cause: err },
315
- );
316
- }
317
- }
318
-
319
- // No media: send text only
320
- return sendTextWithMedia(ctx, messageText, undefined);
321
- }
322
-
323
- /**
324
- * Send a text message with optional base64 media URL.
325
- */
326
- async function sendTextWithMedia(
327
- ctx: MSTeamsProactiveContext,
328
- text: string,
329
- mediaUrl: string | undefined,
330
- ): Promise<SendMSTeamsMessageResult> {
331
- const {
332
- adapter,
333
- appId,
334
- conversationId,
335
- ref,
336
- log,
337
- tokenProvider,
338
- sharePointSiteId,
339
- mediaMaxBytes,
340
- } = ctx;
341
-
342
- let messageIds: string[];
343
- try {
344
- messageIds = await sendMSTeamsMessages({
345
- replyStyle: "top-level",
346
- adapter,
347
- appId,
348
- conversationRef: ref,
349
- messages: [{ text: text || undefined, mediaUrl }],
350
- retry: {},
351
- onRetry: (event) => {
352
- log.debug?.("retrying send", { conversationId, ...event });
353
- },
354
- tokenProvider,
355
- sharePointSiteId,
356
- mediaMaxBytes,
357
- });
358
- } catch (err) {
359
- const classification = classifyMSTeamsSendError(err);
360
- const hint = formatMSTeamsSendErrorHint(classification);
361
- const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
362
- throw new Error(
363
- `msteams send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
364
- { cause: err },
365
- );
366
- }
367
-
368
- const messageId = messageIds[0] ?? "unknown";
369
- log.info("sent proactive message", { conversationId, messageId });
370
-
371
- return {
372
- messageId,
373
- conversationId,
374
- };
375
- }
376
-
377
- type ProactiveActivityParams = {
378
- adapter: MSTeamsProactiveContext["adapter"];
379
- appId: string;
380
- ref: MSTeamsProactiveContext["ref"];
381
- activity: Record<string, unknown>;
382
- errorPrefix: string;
383
- };
384
-
385
- type ProactiveActivityRawParams = Omit<ProactiveActivityParams, "errorPrefix">;
386
-
387
- async function sendProactiveActivityRaw({
388
- adapter,
389
- appId,
390
- ref,
391
- activity,
392
- }: ProactiveActivityRawParams): Promise<string> {
393
- const baseRef = buildConversationReference(ref);
394
- const proactiveRef = {
395
- ...baseRef,
396
- activityId: undefined,
397
- };
398
-
399
- let messageId = "unknown";
400
- await adapter.continueConversation(appId, proactiveRef, async (ctx) => {
401
- const response = await ctx.sendActivity(activity);
402
- messageId = extractMessageId(response) ?? "unknown";
403
- });
404
- return messageId;
405
- }
406
-
407
- async function sendProactiveActivity({
408
- adapter,
409
- appId,
410
- ref,
411
- activity,
412
- errorPrefix,
413
- }: ProactiveActivityParams): Promise<string> {
414
- try {
415
- return await sendProactiveActivityRaw({
416
- adapter,
417
- appId,
418
- ref,
419
- activity,
420
- });
421
- } catch (err) {
422
- const classification = classifyMSTeamsSendError(err);
423
- const hint = formatMSTeamsSendErrorHint(classification);
424
- const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
425
- throw new Error(
426
- `${errorPrefix} failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
427
- { cause: err },
428
- );
429
- }
430
- }
431
-
432
- /**
433
- * Send a poll (Adaptive Card) to a Teams conversation or user.
434
- */
435
- export async function sendPollMSTeams(
436
- params: SendMSTeamsPollParams,
437
- ): Promise<SendMSTeamsPollResult> {
438
- const { cfg, to, question, options, maxSelections } = params;
439
- const { adapter, appId, conversationId, ref, log } = await resolveMSTeamsSendContext({
440
- cfg,
441
- to,
442
- });
443
-
444
- const pollCard = buildMSTeamsPollCard({
445
- question,
446
- options,
447
- maxSelections,
448
- });
449
-
450
- log.debug?.("sending poll", {
451
- conversationId,
452
- pollId: pollCard.pollId,
453
- optionCount: pollCard.options.length,
454
- });
455
-
456
- const activity = {
457
- type: "message",
458
- attachments: [
459
- {
460
- contentType: "application/vnd.microsoft.card.adaptive",
461
- content: pollCard.card,
462
- },
463
- ],
464
- };
465
-
466
- // Send poll via proactive conversation (Adaptive Cards require direct activity send)
467
- const messageId = await sendProactiveActivity({
468
- adapter,
469
- appId,
470
- ref,
471
- activity,
472
- errorPrefix: "msteams poll send",
473
- });
474
-
475
- log.info("sent poll", { conversationId, pollId: pollCard.pollId, messageId });
476
-
477
- return {
478
- pollId: pollCard.pollId,
479
- messageId,
480
- conversationId,
481
- };
482
- }
483
-
484
- /**
485
- * Send an arbitrary Adaptive Card to a Teams conversation or user.
486
- */
487
- export async function sendAdaptiveCardMSTeams(
488
- params: SendMSTeamsCardParams,
489
- ): Promise<SendMSTeamsCardResult> {
490
- const { cfg, to, card } = params;
491
- const { adapter, appId, conversationId, ref, log } = await resolveMSTeamsSendContext({
492
- cfg,
493
- to,
494
- });
495
-
496
- log.debug?.("sending adaptive card", {
497
- conversationId,
498
- cardType: card.type,
499
- cardVersion: card.version,
500
- });
501
-
502
- const activity = {
503
- type: "message",
504
- attachments: [
505
- {
506
- contentType: "application/vnd.microsoft.card.adaptive",
507
- content: card,
508
- },
509
- ],
510
- };
511
-
512
- // Send card via proactive conversation
513
- const messageId = await sendProactiveActivity({
514
- adapter,
515
- appId,
516
- ref,
517
- activity,
518
- errorPrefix: "msteams card send",
519
- });
520
-
521
- log.info("sent adaptive card", { conversationId, messageId });
522
-
523
- return {
524
- messageId,
525
- conversationId,
526
- };
527
- }
528
-
529
- type EditMSTeamsMessageParams = {
530
- /** Full config (for credentials) */
531
- cfg: OpenClawConfig;
532
- /** Conversation ID or user ID */
533
- to: string;
534
- /** Activity ID of the message to edit */
535
- activityId: string;
536
- /** New message text */
537
- text: string;
538
- };
539
-
540
- type EditMSTeamsMessageResult = {
541
- conversationId: string;
542
- };
543
-
544
- type DeleteMSTeamsMessageParams = {
545
- /** Full config (for credentials) */
546
- cfg: OpenClawConfig;
547
- /** Conversation ID or user ID */
548
- to: string;
549
- /** Activity ID of the message to delete */
550
- activityId: string;
551
- };
552
-
553
- type DeleteMSTeamsMessageResult = {
554
- conversationId: string;
555
- };
556
-
557
- /**
558
- * Edit (update) a previously sent message in a Teams conversation.
559
- *
560
- * Uses the Bot Framework `continueConversation` → `updateActivity` flow
561
- * for proactive edits outside of the original turn context.
562
- */
563
- export async function editMessageMSTeams(
564
- params: EditMSTeamsMessageParams,
565
- ): Promise<EditMSTeamsMessageResult> {
566
- const { cfg, to, activityId, text } = params;
567
- const { adapter, appId, conversationId, ref, log } = await resolveMSTeamsSendContext({
568
- cfg,
569
- to,
570
- });
571
-
572
- log.debug?.("editing proactive message", { conversationId, activityId, textLength: text.length });
573
-
574
- const baseRef = buildConversationReference(ref);
575
- const proactiveRef = { ...baseRef, activityId: undefined };
576
-
577
- try {
578
- await adapter.continueConversation(appId, proactiveRef, async (ctx) => {
579
- await ctx.updateActivity({
580
- type: "message",
581
- id: activityId,
582
- text,
583
- });
584
- });
585
- } catch (err) {
586
- const classification = classifyMSTeamsSendError(err);
587
- const hint = formatMSTeamsSendErrorHint(classification);
588
- const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
589
- throw new Error(
590
- `msteams edit failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
591
- { cause: err },
592
- );
593
- }
594
-
595
- log.info("edited proactive message", { conversationId, activityId });
596
-
597
- return { conversationId };
598
- }
599
-
600
- /**
601
- * Delete a previously sent message in a Teams conversation.
602
- *
603
- * Uses the Bot Framework `continueConversation` → `deleteActivity` flow
604
- * for proactive deletes outside of the original turn context.
605
- */
606
- export async function deleteMessageMSTeams(
607
- params: DeleteMSTeamsMessageParams,
608
- ): Promise<DeleteMSTeamsMessageResult> {
609
- const { cfg, to, activityId } = params;
610
- const { adapter, appId, conversationId, ref, log } = await resolveMSTeamsSendContext({
611
- cfg,
612
- to,
613
- });
614
-
615
- log.debug?.("deleting proactive message", { conversationId, activityId });
616
-
617
- const baseRef = buildConversationReference(ref);
618
- const proactiveRef = { ...baseRef, activityId: undefined };
619
-
620
- try {
621
- await adapter.continueConversation(appId, proactiveRef, async (ctx) => {
622
- await ctx.deleteActivity(activityId);
623
- });
624
- } catch (err) {
625
- const classification = classifyMSTeamsSendError(err);
626
- const hint = formatMSTeamsSendErrorHint(classification);
627
- const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
628
- throw new Error(
629
- `msteams delete failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
630
- { cause: err },
631
- );
632
- }
633
-
634
- log.info("deleted proactive message", { conversationId, activityId });
635
-
636
- return { conversationId };
637
- }
@@ -1,15 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- clearMSTeamsSentMessageCache,
4
- recordMSTeamsSentMessage,
5
- wasMSTeamsMessageSent,
6
- } from "./sent-message-cache.js";
7
-
8
- describe("msteams sent message cache", () => {
9
- it("records and resolves sent message ids", () => {
10
- clearMSTeamsSentMessageCache();
11
- recordMSTeamsSentMessage("conv-1", "msg-1");
12
- expect(wasMSTeamsMessageSent("conv-1", "msg-1")).toBe(true);
13
- expect(wasMSTeamsMessageSent("conv-1", "msg-2")).toBe(false);
14
- });
15
- });
@@ -1,56 +0,0 @@
1
- const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
2
- const MSTEAMS_SENT_MESSAGES_KEY = Symbol.for("openclaw.msteamsSentMessages");
3
-
4
- let sentMessageCache: Map<string, Map<string, number>> | undefined;
5
-
6
- function getSentMessageCache(): Map<string, Map<string, number>> {
7
- if (!sentMessageCache) {
8
- const globalStore = globalThis as Record<PropertyKey, unknown>;
9
- sentMessageCache =
10
- (globalStore[MSTEAMS_SENT_MESSAGES_KEY] as Map<string, Map<string, number>> | undefined) ??
11
- new Map<string, Map<string, number>>();
12
- globalStore[MSTEAMS_SENT_MESSAGES_KEY] = sentMessageCache;
13
- }
14
- return sentMessageCache;
15
- }
16
-
17
- function cleanupExpired(scopeKey: string, entry: Map<string, number>, now: number): void {
18
- for (const [id, timestamp] of entry) {
19
- if (now - timestamp > TTL_MS) {
20
- entry.delete(id);
21
- }
22
- }
23
- if (entry.size === 0) {
24
- getSentMessageCache().delete(scopeKey);
25
- }
26
- }
27
-
28
- export function recordMSTeamsSentMessage(conversationId: string, messageId: string): void {
29
- if (!conversationId || !messageId) {
30
- return;
31
- }
32
- const now = Date.now();
33
- const store = getSentMessageCache();
34
- let entry = store.get(conversationId);
35
- if (!entry) {
36
- entry = new Map<string, number>();
37
- store.set(conversationId, entry);
38
- }
39
- entry.set(messageId, now);
40
- if (entry.size > 200) {
41
- cleanupExpired(conversationId, entry, now);
42
- }
43
- }
44
-
45
- export function wasMSTeamsMessageSent(conversationId: string, messageId: string): boolean {
46
- const entry = getSentMessageCache().get(conversationId);
47
- if (!entry) {
48
- return false;
49
- }
50
- cleanupExpired(conversationId, entry, Date.now());
51
- return entry.has(messageId);
52
- }
53
-
54
- export function clearMSTeamsSentMessageCache(): void {
55
- getSentMessageCache().clear();
56
- }