@openclaw/msteams 2026.1.29

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 (61) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/index.ts +18 -0
  3. package/openclaw.plugin.json +11 -0
  4. package/package.json +36 -0
  5. package/src/attachments/download.ts +206 -0
  6. package/src/attachments/graph.ts +319 -0
  7. package/src/attachments/html.ts +76 -0
  8. package/src/attachments/payload.ts +22 -0
  9. package/src/attachments/shared.ts +235 -0
  10. package/src/attachments/types.ts +37 -0
  11. package/src/attachments.test.ts +424 -0
  12. package/src/attachments.ts +18 -0
  13. package/src/channel.directory.test.ts +46 -0
  14. package/src/channel.ts +436 -0
  15. package/src/conversation-store-fs.test.ts +89 -0
  16. package/src/conversation-store-fs.ts +155 -0
  17. package/src/conversation-store-memory.ts +45 -0
  18. package/src/conversation-store.ts +41 -0
  19. package/src/directory-live.ts +179 -0
  20. package/src/errors.test.ts +46 -0
  21. package/src/errors.ts +158 -0
  22. package/src/file-consent-helpers.test.ts +234 -0
  23. package/src/file-consent-helpers.ts +73 -0
  24. package/src/file-consent.ts +122 -0
  25. package/src/graph-chat.ts +52 -0
  26. package/src/graph-upload.ts +445 -0
  27. package/src/inbound.test.ts +67 -0
  28. package/src/inbound.ts +38 -0
  29. package/src/index.ts +4 -0
  30. package/src/media-helpers.test.ts +186 -0
  31. package/src/media-helpers.ts +77 -0
  32. package/src/messenger.test.ts +245 -0
  33. package/src/messenger.ts +460 -0
  34. package/src/monitor-handler/inbound-media.ts +123 -0
  35. package/src/monitor-handler/message-handler.ts +629 -0
  36. package/src/monitor-handler.ts +166 -0
  37. package/src/monitor-types.ts +5 -0
  38. package/src/monitor.ts +290 -0
  39. package/src/onboarding.ts +432 -0
  40. package/src/outbound.ts +47 -0
  41. package/src/pending-uploads.ts +87 -0
  42. package/src/policy.test.ts +210 -0
  43. package/src/policy.ts +247 -0
  44. package/src/polls-store-memory.ts +30 -0
  45. package/src/polls-store.test.ts +40 -0
  46. package/src/polls.test.ts +73 -0
  47. package/src/polls.ts +300 -0
  48. package/src/probe.test.ts +57 -0
  49. package/src/probe.ts +99 -0
  50. package/src/reply-dispatcher.ts +128 -0
  51. package/src/resolve-allowlist.ts +277 -0
  52. package/src/runtime.ts +14 -0
  53. package/src/sdk-types.ts +19 -0
  54. package/src/sdk.ts +33 -0
  55. package/src/send-context.ts +156 -0
  56. package/src/send.ts +489 -0
  57. package/src/sent-message-cache.test.ts +16 -0
  58. package/src/sent-message-cache.ts +41 -0
  59. package/src/storage.ts +22 -0
  60. package/src/store-fs.ts +80 -0
  61. package/src/token.ts +19 -0
package/src/send.ts ADDED
@@ -0,0 +1,489 @@
1
+ import { loadWebMedia, resolveChannelMediaMaxBytes } from "openclaw/plugin-sdk";
2
+ import type { OpenClawConfig } from "openclaw/plugin-sdk";
3
+ import { createMSTeamsConversationStoreFs } from "./conversation-store-fs.js";
4
+ import {
5
+ classifyMSTeamsSendError,
6
+ formatMSTeamsSendErrorHint,
7
+ formatUnknownError,
8
+ } from "./errors.js";
9
+ import { prepareFileConsentActivity, 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 { buildMSTeamsPollCard } from "./polls.js";
19
+ import { getMSTeamsRuntime } from "./runtime.js";
20
+ import { resolveMSTeamsSendContext, type MSTeamsProactiveContext } from "./send-context.js";
21
+
22
+ export type SendMSTeamsMessageParams = {
23
+ /** Full config (for credentials) */
24
+ cfg: OpenClawConfig;
25
+ /** Conversation ID or user ID to send to */
26
+ to: string;
27
+ /** Message text */
28
+ text: string;
29
+ /** Optional media URL */
30
+ mediaUrl?: string;
31
+ };
32
+
33
+ export type SendMSTeamsMessageResult = {
34
+ messageId: string;
35
+ conversationId: string;
36
+ /** If a FileConsentCard was sent instead of the file, this contains the upload ID */
37
+ pendingUploadId?: string;
38
+ };
39
+
40
+ /** Threshold for large files that require FileConsentCard flow in personal chats */
41
+ const FILE_CONSENT_THRESHOLD_BYTES = 4 * 1024 * 1024; // 4MB
42
+
43
+ /**
44
+ * MSTeams-specific media size limit (100MB).
45
+ * Higher than the default because OneDrive upload handles large files well.
46
+ */
47
+ const MSTEAMS_MAX_MEDIA_BYTES = 100 * 1024 * 1024;
48
+
49
+ export type SendMSTeamsPollParams = {
50
+ /** Full config (for credentials) */
51
+ cfg: OpenClawConfig;
52
+ /** Conversation ID or user ID to send to */
53
+ to: string;
54
+ /** Poll question */
55
+ question: string;
56
+ /** Poll options */
57
+ options: string[];
58
+ /** Max selections (defaults to 1) */
59
+ maxSelections?: number;
60
+ };
61
+
62
+ export type SendMSTeamsPollResult = {
63
+ pollId: string;
64
+ messageId: string;
65
+ conversationId: string;
66
+ };
67
+
68
+ export type SendMSTeamsCardParams = {
69
+ /** Full config (for credentials) */
70
+ cfg: OpenClawConfig;
71
+ /** Conversation ID or user ID to send to */
72
+ to: string;
73
+ /** Adaptive Card JSON object */
74
+ card: Record<string, unknown>;
75
+ };
76
+
77
+ export type SendMSTeamsCardResult = {
78
+ messageId: string;
79
+ conversationId: string;
80
+ };
81
+
82
+ /**
83
+ * Send a message to a Teams conversation or user.
84
+ *
85
+ * Uses the stored ConversationReference from previous interactions.
86
+ * The bot must have received at least one message from the conversation
87
+ * before proactive messaging works.
88
+ *
89
+ * File handling by conversation type:
90
+ * - Personal (1:1) chats: small images (<4MB) use base64, large files and non-images use FileConsentCard
91
+ * - Group chats / channels: files are uploaded to OneDrive and shared via link
92
+ */
93
+ export async function sendMessageMSTeams(
94
+ params: SendMSTeamsMessageParams,
95
+ ): Promise<SendMSTeamsMessageResult> {
96
+ const { cfg, to, text, mediaUrl } = params;
97
+ const tableMode = getMSTeamsRuntime().channel.text.resolveMarkdownTableMode({
98
+ cfg,
99
+ channel: "msteams",
100
+ });
101
+ const messageText = getMSTeamsRuntime().channel.text.convertMarkdownTables(
102
+ text ?? "",
103
+ tableMode,
104
+ );
105
+ const ctx = await resolveMSTeamsSendContext({ cfg, to });
106
+ const { adapter, appId, conversationId, ref, log, conversationType, tokenProvider, sharePointSiteId } = ctx;
107
+
108
+ log.debug("sending proactive message", {
109
+ conversationId,
110
+ conversationType,
111
+ textLength: messageText.length,
112
+ hasMedia: Boolean(mediaUrl),
113
+ });
114
+
115
+ // Handle media if present
116
+ if (mediaUrl) {
117
+ const mediaMaxBytes = resolveChannelMediaMaxBytes({
118
+ cfg,
119
+ resolveChannelLimitMb: ({ cfg }) => cfg.channels?.msteams?.mediaMaxMb,
120
+ }) ?? MSTEAMS_MAX_MEDIA_BYTES;
121
+ const media = await loadWebMedia(mediaUrl, mediaMaxBytes);
122
+ const isLargeFile = media.buffer.length >= FILE_CONSENT_THRESHOLD_BYTES;
123
+ const isImage = media.contentType?.startsWith("image/") ?? false;
124
+ const fallbackFileName = await extractFilename(mediaUrl);
125
+ const fileName = media.fileName ?? fallbackFileName;
126
+
127
+ log.debug("processing media", {
128
+ fileName,
129
+ contentType: media.contentType,
130
+ size: media.buffer.length,
131
+ isLargeFile,
132
+ isImage,
133
+ conversationType,
134
+ });
135
+
136
+ // Personal chats: base64 only works for images; use FileConsentCard for large files or non-images
137
+ if (requiresFileConsent({
138
+ conversationType,
139
+ contentType: media.contentType,
140
+ bufferSize: media.buffer.length,
141
+ thresholdBytes: FILE_CONSENT_THRESHOLD_BYTES,
142
+ })) {
143
+ const { activity, uploadId } = prepareFileConsentActivity({
144
+ media: { buffer: media.buffer, filename: fileName, contentType: media.contentType },
145
+ conversationId,
146
+ description: messageText || undefined,
147
+ });
148
+
149
+ log.debug("sending file consent card", { uploadId, fileName, size: media.buffer.length });
150
+
151
+ const baseRef = buildConversationReference(ref);
152
+ const proactiveRef = { ...baseRef, activityId: undefined };
153
+
154
+ let messageId = "unknown";
155
+ try {
156
+ await adapter.continueConversation(appId, proactiveRef, async (turnCtx) => {
157
+ const response = await turnCtx.sendActivity(activity);
158
+ messageId = extractMessageId(response) ?? "unknown";
159
+ });
160
+ } catch (err) {
161
+ const classification = classifyMSTeamsSendError(err);
162
+ const hint = formatMSTeamsSendErrorHint(classification);
163
+ const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
164
+ throw new Error(
165
+ `msteams consent card send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
166
+ );
167
+ }
168
+
169
+ log.info("sent file consent card", { conversationId, messageId, uploadId });
170
+
171
+ return {
172
+ messageId,
173
+ conversationId,
174
+ pendingUploadId: uploadId,
175
+ };
176
+ }
177
+
178
+ // Personal chat with small image: use base64 (only works for images)
179
+ if (conversationType === "personal") {
180
+ // Small image in personal chat: use base64 (only works for images)
181
+ const base64 = media.buffer.toString("base64");
182
+ const finalMediaUrl = `data:${media.contentType};base64,${base64}`;
183
+
184
+ return sendTextWithMedia(ctx, messageText, finalMediaUrl);
185
+ }
186
+
187
+ if (isImage && !sharePointSiteId) {
188
+ // Group chat/channel without SharePoint: send image inline (avoids OneDrive failures)
189
+ const base64 = media.buffer.toString("base64");
190
+ const finalMediaUrl = `data:${media.contentType};base64,${base64}`;
191
+ return sendTextWithMedia(ctx, messageText, finalMediaUrl);
192
+ }
193
+
194
+ // Group chat or channel: upload to SharePoint (if siteId configured) or OneDrive
195
+ try {
196
+ if (sharePointSiteId) {
197
+ // Use SharePoint upload + Graph API for native file card
198
+ log.debug("uploading to SharePoint for native file card", {
199
+ fileName,
200
+ conversationType,
201
+ siteId: sharePointSiteId,
202
+ });
203
+
204
+ const uploaded = await uploadAndShareSharePoint({
205
+ buffer: media.buffer,
206
+ filename: fileName,
207
+ contentType: media.contentType,
208
+ tokenProvider,
209
+ siteId: sharePointSiteId,
210
+ chatId: conversationId,
211
+ usePerUserSharing: conversationType === "groupChat",
212
+ });
213
+
214
+ log.debug("SharePoint upload complete", {
215
+ itemId: uploaded.itemId,
216
+ shareUrl: uploaded.shareUrl,
217
+ });
218
+
219
+ // Get driveItem properties needed for native file card
220
+ const driveItem = await getDriveItemProperties({
221
+ siteId: sharePointSiteId,
222
+ itemId: uploaded.itemId,
223
+ tokenProvider,
224
+ });
225
+
226
+ log.debug("driveItem properties retrieved", {
227
+ eTag: driveItem.eTag,
228
+ webDavUrl: driveItem.webDavUrl,
229
+ });
230
+
231
+ // Build native Teams file card attachment and send via Bot Framework
232
+ const fileCardAttachment = buildTeamsFileInfoCard(driveItem);
233
+ const activity = {
234
+ type: "message",
235
+ text: messageText || undefined,
236
+ attachments: [fileCardAttachment],
237
+ };
238
+
239
+ const baseRef = buildConversationReference(ref);
240
+ const proactiveRef = { ...baseRef, activityId: undefined };
241
+
242
+ let messageId = "unknown";
243
+ await adapter.continueConversation(appId, proactiveRef, async (turnCtx) => {
244
+ const response = await turnCtx.sendActivity(activity);
245
+ messageId = extractMessageId(response) ?? "unknown";
246
+ });
247
+
248
+ log.info("sent native file card", {
249
+ conversationId,
250
+ messageId,
251
+ fileName: driveItem.name,
252
+ });
253
+
254
+ return { messageId, conversationId };
255
+ }
256
+
257
+ // Fallback: no SharePoint site configured, use OneDrive with markdown link
258
+ log.debug("uploading to OneDrive (no SharePoint site configured)", { fileName, conversationType });
259
+
260
+ const uploaded = await uploadAndShareOneDrive({
261
+ buffer: media.buffer,
262
+ filename: fileName,
263
+ contentType: media.contentType,
264
+ tokenProvider,
265
+ });
266
+
267
+ log.debug("OneDrive upload complete", {
268
+ itemId: uploaded.itemId,
269
+ shareUrl: uploaded.shareUrl,
270
+ });
271
+
272
+ // Send message with file link (Bot Framework doesn't support "reference" attachment type for sending)
273
+ const fileLink = `📎 [${uploaded.name}](${uploaded.shareUrl})`;
274
+ const activity = {
275
+ type: "message",
276
+ text: messageText ? `${messageText}\n\n${fileLink}` : fileLink,
277
+ };
278
+
279
+ const baseRef = buildConversationReference(ref);
280
+ const proactiveRef = { ...baseRef, activityId: undefined };
281
+
282
+ let messageId = "unknown";
283
+ await adapter.continueConversation(appId, proactiveRef, async (turnCtx) => {
284
+ const response = await turnCtx.sendActivity(activity);
285
+ messageId = extractMessageId(response) ?? "unknown";
286
+ });
287
+
288
+ log.info("sent message with OneDrive file link", { conversationId, messageId, shareUrl: uploaded.shareUrl });
289
+
290
+ return { messageId, conversationId };
291
+ } catch (err) {
292
+ const classification = classifyMSTeamsSendError(err);
293
+ const hint = formatMSTeamsSendErrorHint(classification);
294
+ const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
295
+ throw new Error(
296
+ `msteams file send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
297
+ );
298
+ }
299
+ }
300
+
301
+ // No media: send text only
302
+ return sendTextWithMedia(ctx, messageText, undefined);
303
+ }
304
+
305
+ /**
306
+ * Send a text message with optional base64 media URL.
307
+ */
308
+ async function sendTextWithMedia(
309
+ ctx: MSTeamsProactiveContext,
310
+ text: string,
311
+ mediaUrl: string | undefined,
312
+ ): Promise<SendMSTeamsMessageResult> {
313
+ const { adapter, appId, conversationId, ref, log, tokenProvider, sharePointSiteId, mediaMaxBytes } = ctx;
314
+
315
+ let messageIds: string[];
316
+ try {
317
+ messageIds = await sendMSTeamsMessages({
318
+ replyStyle: "top-level",
319
+ adapter,
320
+ appId,
321
+ conversationRef: ref,
322
+ messages: [{ text: text || undefined, mediaUrl }],
323
+ retry: {},
324
+ onRetry: (event) => {
325
+ log.debug("retrying send", { conversationId, ...event });
326
+ },
327
+ tokenProvider,
328
+ sharePointSiteId,
329
+ mediaMaxBytes,
330
+ });
331
+ } catch (err) {
332
+ const classification = classifyMSTeamsSendError(err);
333
+ const hint = formatMSTeamsSendErrorHint(classification);
334
+ const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
335
+ throw new Error(
336
+ `msteams send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
337
+ );
338
+ }
339
+
340
+ const messageId = messageIds[0] ?? "unknown";
341
+ log.info("sent proactive message", { conversationId, messageId });
342
+
343
+ return {
344
+ messageId,
345
+ conversationId,
346
+ };
347
+ }
348
+
349
+ /**
350
+ * Send a poll (Adaptive Card) to a Teams conversation or user.
351
+ */
352
+ export async function sendPollMSTeams(
353
+ params: SendMSTeamsPollParams,
354
+ ): Promise<SendMSTeamsPollResult> {
355
+ const { cfg, to, question, options, maxSelections } = params;
356
+ const { adapter, appId, conversationId, ref, log } = await resolveMSTeamsSendContext({
357
+ cfg,
358
+ to,
359
+ });
360
+
361
+ const pollCard = buildMSTeamsPollCard({
362
+ question,
363
+ options,
364
+ maxSelections,
365
+ });
366
+
367
+ log.debug("sending poll", {
368
+ conversationId,
369
+ pollId: pollCard.pollId,
370
+ optionCount: pollCard.options.length,
371
+ });
372
+
373
+ const activity = {
374
+ type: "message",
375
+ attachments: [
376
+ {
377
+ contentType: "application/vnd.microsoft.card.adaptive",
378
+ content: pollCard.card,
379
+ },
380
+ ],
381
+ };
382
+
383
+ // Send poll via proactive conversation (Adaptive Cards require direct activity send)
384
+ const baseRef = buildConversationReference(ref);
385
+ const proactiveRef = {
386
+ ...baseRef,
387
+ activityId: undefined,
388
+ };
389
+
390
+ let messageId = "unknown";
391
+ try {
392
+ await adapter.continueConversation(appId, proactiveRef, async (ctx) => {
393
+ const response = await ctx.sendActivity(activity);
394
+ messageId = extractMessageId(response) ?? "unknown";
395
+ });
396
+ } catch (err) {
397
+ const classification = classifyMSTeamsSendError(err);
398
+ const hint = formatMSTeamsSendErrorHint(classification);
399
+ const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
400
+ throw new Error(
401
+ `msteams poll send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
402
+ );
403
+ }
404
+
405
+ log.info("sent poll", { conversationId, pollId: pollCard.pollId, messageId });
406
+
407
+ return {
408
+ pollId: pollCard.pollId,
409
+ messageId,
410
+ conversationId,
411
+ };
412
+ }
413
+
414
+ /**
415
+ * Send an arbitrary Adaptive Card to a Teams conversation or user.
416
+ */
417
+ export async function sendAdaptiveCardMSTeams(
418
+ params: SendMSTeamsCardParams,
419
+ ): Promise<SendMSTeamsCardResult> {
420
+ const { cfg, to, card } = params;
421
+ const { adapter, appId, conversationId, ref, log } = await resolveMSTeamsSendContext({
422
+ cfg,
423
+ to,
424
+ });
425
+
426
+ log.debug("sending adaptive card", {
427
+ conversationId,
428
+ cardType: card.type,
429
+ cardVersion: card.version,
430
+ });
431
+
432
+ const activity = {
433
+ type: "message",
434
+ attachments: [
435
+ {
436
+ contentType: "application/vnd.microsoft.card.adaptive",
437
+ content: card,
438
+ },
439
+ ],
440
+ };
441
+
442
+ // Send card via proactive conversation
443
+ const baseRef = buildConversationReference(ref);
444
+ const proactiveRef = {
445
+ ...baseRef,
446
+ activityId: undefined,
447
+ };
448
+
449
+ let messageId = "unknown";
450
+ try {
451
+ await adapter.continueConversation(appId, proactiveRef, async (ctx) => {
452
+ const response = await ctx.sendActivity(activity);
453
+ messageId = extractMessageId(response) ?? "unknown";
454
+ });
455
+ } catch (err) {
456
+ const classification = classifyMSTeamsSendError(err);
457
+ const hint = formatMSTeamsSendErrorHint(classification);
458
+ const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
459
+ throw new Error(
460
+ `msteams card send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
461
+ );
462
+ }
463
+
464
+ log.info("sent adaptive card", { conversationId, messageId });
465
+
466
+ return {
467
+ messageId,
468
+ conversationId,
469
+ };
470
+ }
471
+
472
+ /**
473
+ * List all known conversation references (for debugging/CLI).
474
+ */
475
+ export async function listMSTeamsConversations(): Promise<
476
+ Array<{
477
+ conversationId: string;
478
+ userName?: string;
479
+ conversationType?: string;
480
+ }>
481
+ > {
482
+ const store = createMSTeamsConversationStoreFs();
483
+ const all = await store.list();
484
+ return all.map(({ conversationId, reference }) => ({
485
+ conversationId,
486
+ userName: reference.user?.name,
487
+ conversationType: reference.conversation?.conversationType,
488
+ }));
489
+ }
@@ -0,0 +1,16 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import {
4
+ clearMSTeamsSentMessageCache,
5
+ recordMSTeamsSentMessage,
6
+ wasMSTeamsMessageSent,
7
+ } from "./sent-message-cache.js";
8
+
9
+ describe("msteams sent message cache", () => {
10
+ it("records and resolves sent message ids", () => {
11
+ clearMSTeamsSentMessageCache();
12
+ recordMSTeamsSentMessage("conv-1", "msg-1");
13
+ expect(wasMSTeamsMessageSent("conv-1", "msg-1")).toBe(true);
14
+ expect(wasMSTeamsMessageSent("conv-1", "msg-2")).toBe(false);
15
+ });
16
+ });
@@ -0,0 +1,41 @@
1
+ const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
2
+
3
+ type CacheEntry = {
4
+ messageIds: Set<string>;
5
+ timestamps: Map<string, number>;
6
+ };
7
+
8
+ const sentMessages = new Map<string, CacheEntry>();
9
+
10
+ function cleanupExpired(entry: CacheEntry): void {
11
+ const now = Date.now();
12
+ for (const [msgId, timestamp] of entry.timestamps) {
13
+ if (now - timestamp > TTL_MS) {
14
+ entry.messageIds.delete(msgId);
15
+ entry.timestamps.delete(msgId);
16
+ }
17
+ }
18
+ }
19
+
20
+ export function recordMSTeamsSentMessage(conversationId: string, messageId: string): void {
21
+ if (!conversationId || !messageId) return;
22
+ let entry = sentMessages.get(conversationId);
23
+ if (!entry) {
24
+ entry = { messageIds: new Set(), timestamps: new Map() };
25
+ sentMessages.set(conversationId, entry);
26
+ }
27
+ entry.messageIds.add(messageId);
28
+ entry.timestamps.set(messageId, Date.now());
29
+ if (entry.messageIds.size > 200) cleanupExpired(entry);
30
+ }
31
+
32
+ export function wasMSTeamsMessageSent(conversationId: string, messageId: string): boolean {
33
+ const entry = sentMessages.get(conversationId);
34
+ if (!entry) return false;
35
+ cleanupExpired(entry);
36
+ return entry.messageIds.has(messageId);
37
+ }
38
+
39
+ export function clearMSTeamsSentMessageCache(): void {
40
+ sentMessages.clear();
41
+ }
package/src/storage.ts ADDED
@@ -0,0 +1,22 @@
1
+ import path from "node:path";
2
+
3
+ import { getMSTeamsRuntime } from "./runtime.js";
4
+
5
+ export type MSTeamsStorePathOptions = {
6
+ env?: NodeJS.ProcessEnv;
7
+ homedir?: () => string;
8
+ stateDir?: string;
9
+ storePath?: string;
10
+ filename: string;
11
+ };
12
+
13
+ export function resolveMSTeamsStorePath(params: MSTeamsStorePathOptions): string {
14
+ if (params.storePath) return params.storePath;
15
+ if (params.stateDir) return path.join(params.stateDir, params.filename);
16
+
17
+ const env = params.env ?? process.env;
18
+ const stateDir = params.homedir
19
+ ? getMSTeamsRuntime().state.resolveStateDir(env, params.homedir)
20
+ : getMSTeamsRuntime().state.resolveStateDir(env);
21
+ return path.join(stateDir, params.filename);
22
+ }
@@ -0,0 +1,80 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+
5
+ import lockfile from "proper-lockfile";
6
+
7
+ const STORE_LOCK_OPTIONS = {
8
+ retries: {
9
+ retries: 10,
10
+ factor: 2,
11
+ minTimeout: 100,
12
+ maxTimeout: 10_000,
13
+ randomize: true,
14
+ },
15
+ stale: 30_000,
16
+ } as const;
17
+
18
+ function safeParseJson<T>(raw: string): T | null {
19
+ try {
20
+ return JSON.parse(raw) as T;
21
+ } catch {
22
+ return null;
23
+ }
24
+ }
25
+
26
+ export async function readJsonFile<T>(
27
+ filePath: string,
28
+ fallback: T,
29
+ ): Promise<{ value: T; exists: boolean }> {
30
+ try {
31
+ const raw = await fs.promises.readFile(filePath, "utf-8");
32
+ const parsed = safeParseJson<T>(raw);
33
+ if (parsed == null) return { value: fallback, exists: true };
34
+ return { value: parsed, exists: true };
35
+ } catch (err) {
36
+ const code = (err as { code?: string }).code;
37
+ if (code === "ENOENT") return { value: fallback, exists: false };
38
+ return { value: fallback, exists: false };
39
+ }
40
+ }
41
+
42
+ export async function writeJsonFile(filePath: string, value: unknown): Promise<void> {
43
+ const dir = path.dirname(filePath);
44
+ await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
45
+ const tmp = path.join(dir, `${path.basename(filePath)}.${crypto.randomUUID()}.tmp`);
46
+ await fs.promises.writeFile(tmp, `${JSON.stringify(value, null, 2)}\n`, {
47
+ encoding: "utf-8",
48
+ });
49
+ await fs.promises.chmod(tmp, 0o600);
50
+ await fs.promises.rename(tmp, filePath);
51
+ }
52
+
53
+ async function ensureJsonFile(filePath: string, fallback: unknown) {
54
+ try {
55
+ await fs.promises.access(filePath);
56
+ } catch {
57
+ await writeJsonFile(filePath, fallback);
58
+ }
59
+ }
60
+
61
+ export async function withFileLock<T>(
62
+ filePath: string,
63
+ fallback: unknown,
64
+ fn: () => Promise<T>,
65
+ ): Promise<T> {
66
+ await ensureJsonFile(filePath, fallback);
67
+ let release: (() => Promise<void>) | undefined;
68
+ try {
69
+ release = await lockfile.lock(filePath, STORE_LOCK_OPTIONS);
70
+ return await fn();
71
+ } finally {
72
+ if (release) {
73
+ try {
74
+ await release();
75
+ } catch {
76
+ // ignore unlock errors
77
+ }
78
+ }
79
+ }
80
+ }
package/src/token.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { MSTeamsConfig } from "openclaw/plugin-sdk";
2
+
3
+ export type MSTeamsCredentials = {
4
+ appId: string;
5
+ appPassword: string;
6
+ tenantId: string;
7
+ };
8
+
9
+ export function resolveMSTeamsCredentials(cfg?: MSTeamsConfig): MSTeamsCredentials | undefined {
10
+ const appId = cfg?.appId?.trim() || process.env.MSTEAMS_APP_ID?.trim();
11
+ const appPassword = cfg?.appPassword?.trim() || process.env.MSTEAMS_APP_PASSWORD?.trim();
12
+ const tenantId = cfg?.tenantId?.trim() || process.env.MSTEAMS_TENANT_ID?.trim();
13
+
14
+ if (!appId || !appPassword || !tenantId) {
15
+ return undefined;
16
+ }
17
+
18
+ return { appId, appPassword, tenantId };
19
+ }