@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
@@ -0,0 +1,76 @@
1
+ import {
2
+ ATTACHMENT_TAG_RE,
3
+ extractHtmlFromAttachment,
4
+ extractInlineImageCandidates,
5
+ IMG_SRC_RE,
6
+ isLikelyImageAttachment,
7
+ safeHostForUrl,
8
+ } from "./shared.js";
9
+ import type { MSTeamsAttachmentLike, MSTeamsHtmlAttachmentSummary } from "./types.js";
10
+
11
+ export function summarizeMSTeamsHtmlAttachments(
12
+ attachments: MSTeamsAttachmentLike[] | undefined,
13
+ ): MSTeamsHtmlAttachmentSummary | undefined {
14
+ const list = Array.isArray(attachments) ? attachments : [];
15
+ if (list.length === 0) return undefined;
16
+ let htmlAttachments = 0;
17
+ let imgTags = 0;
18
+ let dataImages = 0;
19
+ let cidImages = 0;
20
+ const srcHosts = new Set<string>();
21
+ let attachmentTags = 0;
22
+ const attachmentIds = new Set<string>();
23
+
24
+ for (const att of list) {
25
+ const html = extractHtmlFromAttachment(att);
26
+ if (!html) continue;
27
+ htmlAttachments += 1;
28
+ IMG_SRC_RE.lastIndex = 0;
29
+ let match: RegExpExecArray | null = IMG_SRC_RE.exec(html);
30
+ while (match) {
31
+ imgTags += 1;
32
+ const src = match[1]?.trim();
33
+ if (src) {
34
+ if (src.startsWith("data:")) dataImages += 1;
35
+ else if (src.startsWith("cid:")) cidImages += 1;
36
+ else srcHosts.add(safeHostForUrl(src));
37
+ }
38
+ match = IMG_SRC_RE.exec(html);
39
+ }
40
+
41
+ ATTACHMENT_TAG_RE.lastIndex = 0;
42
+ let attachmentMatch: RegExpExecArray | null = ATTACHMENT_TAG_RE.exec(html);
43
+ while (attachmentMatch) {
44
+ attachmentTags += 1;
45
+ const id = attachmentMatch[1]?.trim();
46
+ if (id) attachmentIds.add(id);
47
+ attachmentMatch = ATTACHMENT_TAG_RE.exec(html);
48
+ }
49
+ }
50
+
51
+ if (htmlAttachments === 0) return undefined;
52
+ return {
53
+ htmlAttachments,
54
+ imgTags,
55
+ dataImages,
56
+ cidImages,
57
+ srcHosts: Array.from(srcHosts).slice(0, 5),
58
+ attachmentTags,
59
+ attachmentIds: Array.from(attachmentIds).slice(0, 5),
60
+ };
61
+ }
62
+
63
+ export function buildMSTeamsAttachmentPlaceholder(
64
+ attachments: MSTeamsAttachmentLike[] | undefined,
65
+ ): string {
66
+ const list = Array.isArray(attachments) ? attachments : [];
67
+ if (list.length === 0) return "";
68
+ const imageCount = list.filter(isLikelyImageAttachment).length;
69
+ const inlineCount = extractInlineImageCandidates(list).length;
70
+ const totalImages = imageCount + inlineCount;
71
+ if (totalImages > 0) {
72
+ return `<media:image>${totalImages > 1 ? ` (${totalImages} images)` : ""}`;
73
+ }
74
+ const count = list.length;
75
+ return `<media:document>${count > 1 ? ` (${count} files)` : ""}`;
76
+ }
@@ -0,0 +1,22 @@
1
+ export function buildMSTeamsMediaPayload(
2
+ mediaList: Array<{ path: string; contentType?: string }>,
3
+ ): {
4
+ MediaPath?: string;
5
+ MediaType?: string;
6
+ MediaUrl?: string;
7
+ MediaPaths?: string[];
8
+ MediaUrls?: string[];
9
+ MediaTypes?: string[];
10
+ } {
11
+ const first = mediaList[0];
12
+ const mediaPaths = mediaList.map((media) => media.path);
13
+ const mediaTypes = mediaList.map((media) => media.contentType ?? "");
14
+ return {
15
+ MediaPath: first?.path,
16
+ MediaType: first?.contentType,
17
+ MediaUrl: first?.path,
18
+ MediaPaths: mediaPaths.length > 0 ? mediaPaths : undefined,
19
+ MediaUrls: mediaPaths.length > 0 ? mediaPaths : undefined,
20
+ MediaTypes: mediaPaths.length > 0 ? mediaTypes : undefined,
21
+ };
22
+ }
@@ -0,0 +1,235 @@
1
+ import type { MSTeamsAttachmentLike } from "./types.js";
2
+
3
+ type InlineImageCandidate =
4
+ | {
5
+ kind: "data";
6
+ data: Buffer;
7
+ contentType?: string;
8
+ placeholder: string;
9
+ }
10
+ | {
11
+ kind: "url";
12
+ url: string;
13
+ contentType?: string;
14
+ fileHint?: string;
15
+ placeholder: string;
16
+ };
17
+
18
+ export const IMAGE_EXT_RE = /\.(avif|bmp|gif|heic|heif|jpe?g|png|tiff?|webp)$/i;
19
+
20
+ export const IMG_SRC_RE = /<img[^>]+src=["']([^"']+)["'][^>]*>/gi;
21
+ export const ATTACHMENT_TAG_RE = /<attachment[^>]+id=["']([^"']+)["'][^>]*>/gi;
22
+
23
+ export const DEFAULT_MEDIA_HOST_ALLOWLIST = [
24
+ "graph.microsoft.com",
25
+ "graph.microsoft.us",
26
+ "graph.microsoft.de",
27
+ "graph.microsoft.cn",
28
+ "sharepoint.com",
29
+ "sharepoint.us",
30
+ "sharepoint.de",
31
+ "sharepoint.cn",
32
+ "sharepoint-df.com",
33
+ "1drv.ms",
34
+ "onedrive.com",
35
+ "teams.microsoft.com",
36
+ "teams.cdn.office.net",
37
+ "statics.teams.cdn.office.net",
38
+ "office.com",
39
+ "office.net",
40
+ // Azure Media Services / Skype CDN for clipboard-pasted images
41
+ "asm.skype.com",
42
+ "ams.skype.com",
43
+ "media.ams.skype.com",
44
+ // Bot Framework attachment URLs
45
+ "trafficmanager.net",
46
+ "blob.core.windows.net",
47
+ "azureedge.net",
48
+ "microsoft.com",
49
+ ] as const;
50
+
51
+ export const GRAPH_ROOT = "https://graph.microsoft.com/v1.0";
52
+
53
+ export function isRecord(value: unknown): value is Record<string, unknown> {
54
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
55
+ }
56
+
57
+ export function normalizeContentType(value: unknown): string | undefined {
58
+ if (typeof value !== "string") return undefined;
59
+ const trimmed = value.trim();
60
+ return trimmed ? trimmed : undefined;
61
+ }
62
+
63
+ export function inferPlaceholder(params: {
64
+ contentType?: string;
65
+ fileName?: string;
66
+ fileType?: string;
67
+ }): string {
68
+ const mime = params.contentType?.toLowerCase() ?? "";
69
+ const name = params.fileName?.toLowerCase() ?? "";
70
+ const fileType = params.fileType?.toLowerCase() ?? "";
71
+
72
+ const looksLikeImage =
73
+ mime.startsWith("image/") || IMAGE_EXT_RE.test(name) || IMAGE_EXT_RE.test(`x.${fileType}`);
74
+
75
+ return looksLikeImage ? "<media:image>" : "<media:document>";
76
+ }
77
+
78
+ export function isLikelyImageAttachment(att: MSTeamsAttachmentLike): boolean {
79
+ const contentType = normalizeContentType(att.contentType) ?? "";
80
+ const name = typeof att.name === "string" ? att.name : "";
81
+ if (contentType.startsWith("image/")) return true;
82
+ if (IMAGE_EXT_RE.test(name)) return true;
83
+
84
+ if (
85
+ contentType === "application/vnd.microsoft.teams.file.download.info" &&
86
+ isRecord(att.content)
87
+ ) {
88
+ const fileType = typeof att.content.fileType === "string" ? att.content.fileType : "";
89
+ if (fileType && IMAGE_EXT_RE.test(`x.${fileType}`)) return true;
90
+ const fileName = typeof att.content.fileName === "string" ? att.content.fileName : "";
91
+ if (fileName && IMAGE_EXT_RE.test(fileName)) return true;
92
+ }
93
+
94
+ return false;
95
+ }
96
+
97
+ /**
98
+ * Returns true if the attachment can be downloaded (any file type).
99
+ * Used when downloading all files, not just images.
100
+ */
101
+ export function isDownloadableAttachment(att: MSTeamsAttachmentLike): boolean {
102
+ const contentType = normalizeContentType(att.contentType) ?? "";
103
+
104
+ // Teams file download info always has a downloadUrl
105
+ if (
106
+ contentType === "application/vnd.microsoft.teams.file.download.info" &&
107
+ isRecord(att.content) &&
108
+ typeof att.content.downloadUrl === "string"
109
+ ) {
110
+ return true;
111
+ }
112
+
113
+ // Any attachment with a contentUrl can be downloaded
114
+ if (typeof att.contentUrl === "string" && att.contentUrl.trim()) {
115
+ return true;
116
+ }
117
+
118
+ return false;
119
+ }
120
+
121
+ function isHtmlAttachment(att: MSTeamsAttachmentLike): boolean {
122
+ const contentType = normalizeContentType(att.contentType) ?? "";
123
+ return contentType.startsWith("text/html");
124
+ }
125
+
126
+ export function extractHtmlFromAttachment(att: MSTeamsAttachmentLike): string | undefined {
127
+ if (!isHtmlAttachment(att)) return undefined;
128
+ if (typeof att.content === "string") return att.content;
129
+ if (!isRecord(att.content)) return undefined;
130
+ const text =
131
+ typeof att.content.text === "string"
132
+ ? att.content.text
133
+ : typeof att.content.body === "string"
134
+ ? att.content.body
135
+ : typeof att.content.content === "string"
136
+ ? att.content.content
137
+ : undefined;
138
+ return text;
139
+ }
140
+
141
+ function decodeDataImage(src: string): InlineImageCandidate | null {
142
+ const match = /^data:(image\/[a-z0-9.+-]+)?(;base64)?,(.*)$/i.exec(src);
143
+ if (!match) return null;
144
+ const contentType = match[1]?.toLowerCase();
145
+ const isBase64 = Boolean(match[2]);
146
+ if (!isBase64) return null;
147
+ const payload = match[3] ?? "";
148
+ if (!payload) return null;
149
+ try {
150
+ const data = Buffer.from(payload, "base64");
151
+ return { kind: "data", data, contentType, placeholder: "<media:image>" };
152
+ } catch {
153
+ return null;
154
+ }
155
+ }
156
+
157
+ function fileHintFromUrl(src: string): string | undefined {
158
+ try {
159
+ const url = new URL(src);
160
+ const name = url.pathname.split("/").pop();
161
+ return name || undefined;
162
+ } catch {
163
+ return undefined;
164
+ }
165
+ }
166
+
167
+ export function extractInlineImageCandidates(
168
+ attachments: MSTeamsAttachmentLike[],
169
+ ): InlineImageCandidate[] {
170
+ const out: InlineImageCandidate[] = [];
171
+ for (const att of attachments) {
172
+ const html = extractHtmlFromAttachment(att);
173
+ if (!html) continue;
174
+ IMG_SRC_RE.lastIndex = 0;
175
+ let match: RegExpExecArray | null = IMG_SRC_RE.exec(html);
176
+ while (match) {
177
+ const src = match[1]?.trim();
178
+ if (src && !src.startsWith("cid:")) {
179
+ if (src.startsWith("data:")) {
180
+ const decoded = decodeDataImage(src);
181
+ if (decoded) out.push(decoded);
182
+ } else {
183
+ out.push({
184
+ kind: "url",
185
+ url: src,
186
+ fileHint: fileHintFromUrl(src),
187
+ placeholder: "<media:image>",
188
+ });
189
+ }
190
+ }
191
+ match = IMG_SRC_RE.exec(html);
192
+ }
193
+ }
194
+ return out;
195
+ }
196
+
197
+ export function safeHostForUrl(url: string): string {
198
+ try {
199
+ return new URL(url).hostname.toLowerCase();
200
+ } catch {
201
+ return "invalid-url";
202
+ }
203
+ }
204
+
205
+ function normalizeAllowHost(value: string): string {
206
+ const trimmed = value.trim().toLowerCase();
207
+ if (!trimmed) return "";
208
+ if (trimmed === "*") return "*";
209
+ return trimmed.replace(/^\*\.?/, "");
210
+ }
211
+
212
+ export function resolveAllowedHosts(input?: string[]): string[] {
213
+ if (!Array.isArray(input) || input.length === 0) {
214
+ return DEFAULT_MEDIA_HOST_ALLOWLIST.slice();
215
+ }
216
+ const normalized = input.map(normalizeAllowHost).filter(Boolean);
217
+ if (normalized.includes("*")) return ["*"];
218
+ return normalized;
219
+ }
220
+
221
+ function isHostAllowed(host: string, allowlist: string[]): boolean {
222
+ if (allowlist.includes("*")) return true;
223
+ const normalized = host.toLowerCase();
224
+ return allowlist.some((entry) => normalized === entry || normalized.endsWith(`.${entry}`));
225
+ }
226
+
227
+ export function isUrlAllowed(url: string, allowlist: string[]): boolean {
228
+ try {
229
+ const parsed = new URL(url);
230
+ if (parsed.protocol !== "https:") return false;
231
+ return isHostAllowed(parsed.hostname, allowlist);
232
+ } catch {
233
+ return false;
234
+ }
235
+ }
@@ -0,0 +1,37 @@
1
+ export type MSTeamsAttachmentLike = {
2
+ contentType?: string | null;
3
+ contentUrl?: string | null;
4
+ name?: string | null;
5
+ thumbnailUrl?: string | null;
6
+ content?: unknown;
7
+ };
8
+
9
+ export type MSTeamsAccessTokenProvider = {
10
+ getAccessToken: (scope: string) => Promise<string>;
11
+ };
12
+
13
+ export type MSTeamsInboundMedia = {
14
+ path: string;
15
+ contentType?: string;
16
+ placeholder: string;
17
+ };
18
+
19
+ export type MSTeamsHtmlAttachmentSummary = {
20
+ htmlAttachments: number;
21
+ imgTags: number;
22
+ dataImages: number;
23
+ cidImages: number;
24
+ srcHosts: string[];
25
+ attachmentTags: number;
26
+ attachmentIds: string[];
27
+ };
28
+
29
+ export type MSTeamsGraphMediaResult = {
30
+ media: MSTeamsInboundMedia[];
31
+ hostedCount?: number;
32
+ attachmentCount?: number;
33
+ hostedStatus?: number;
34
+ attachmentStatus?: number;
35
+ messageUrl?: string;
36
+ tokenError?: boolean;
37
+ };