@gakr-gakr/msteams 0.1.0

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 (107) hide show
  1. package/api.ts +3 -0
  2. package/autobot.plugin.json +15 -0
  3. package/channel-config-api.ts +1 -0
  4. package/channel-plugin-api.ts +2 -0
  5. package/config-api.ts +4 -0
  6. package/contract-api.ts +4 -0
  7. package/index.ts +20 -0
  8. package/package.json +72 -0
  9. package/runtime-api.ts +66 -0
  10. package/secret-contract-api.ts +5 -0
  11. package/setup-entry.ts +13 -0
  12. package/setup-plugin-api.ts +3 -0
  13. package/src/ai-entity.ts +7 -0
  14. package/src/approval-auth.ts +44 -0
  15. package/src/attachments/bot-framework.ts +348 -0
  16. package/src/attachments/download.ts +328 -0
  17. package/src/attachments/graph.ts +489 -0
  18. package/src/attachments/html.ts +122 -0
  19. package/src/attachments/payload.ts +14 -0
  20. package/src/attachments/remote-media.ts +86 -0
  21. package/src/attachments/shared.ts +655 -0
  22. package/src/attachments/types.ts +47 -0
  23. package/src/attachments.ts +18 -0
  24. package/src/channel-api.ts +1 -0
  25. package/src/channel.runtime.ts +56 -0
  26. package/src/channel.setup.ts +77 -0
  27. package/src/channel.ts +1176 -0
  28. package/src/config-schema.ts +6 -0
  29. package/src/config-ui-hints.ts +40 -0
  30. package/src/conversation-store-fs.ts +149 -0
  31. package/src/conversation-store-helpers.ts +105 -0
  32. package/src/conversation-store-memory.ts +51 -0
  33. package/src/conversation-store.ts +71 -0
  34. package/src/directory-live.ts +111 -0
  35. package/src/doctor.ts +27 -0
  36. package/src/errors.ts +270 -0
  37. package/src/feedback-reflection-prompt.ts +117 -0
  38. package/src/feedback-reflection-store.ts +113 -0
  39. package/src/feedback-reflection.ts +271 -0
  40. package/src/file-consent-helpers.ts +115 -0
  41. package/src/file-consent-invoke.ts +150 -0
  42. package/src/file-consent.ts +223 -0
  43. package/src/graph-chat.ts +36 -0
  44. package/src/graph-group-management.ts +168 -0
  45. package/src/graph-members.ts +48 -0
  46. package/src/graph-messages.ts +534 -0
  47. package/src/graph-teams.ts +114 -0
  48. package/src/graph-thread.ts +146 -0
  49. package/src/graph-upload.ts +531 -0
  50. package/src/graph-users.ts +29 -0
  51. package/src/graph.ts +308 -0
  52. package/src/inbound.ts +148 -0
  53. package/src/index.ts +4 -0
  54. package/src/media-helpers.ts +105 -0
  55. package/src/mentions.ts +114 -0
  56. package/src/messenger.ts +608 -0
  57. package/src/monitor-handler/access.ts +136 -0
  58. package/src/monitor-handler/inbound-media.ts +180 -0
  59. package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
  60. package/src/monitor-handler/message-handler.test-support.ts +102 -0
  61. package/src/monitor-handler/message-handler.ts +1015 -0
  62. package/src/monitor-handler/reaction-handler.ts +124 -0
  63. package/src/monitor-handler/thread-session.ts +30 -0
  64. package/src/monitor-handler.ts +538 -0
  65. package/src/monitor-handler.types.ts +27 -0
  66. package/src/monitor-types.ts +6 -0
  67. package/src/monitor.ts +476 -0
  68. package/src/oauth.flow.ts +77 -0
  69. package/src/oauth.shared.ts +37 -0
  70. package/src/oauth.token.ts +162 -0
  71. package/src/oauth.ts +130 -0
  72. package/src/outbound.ts +198 -0
  73. package/src/pending-uploads-fs.ts +235 -0
  74. package/src/pending-uploads.ts +121 -0
  75. package/src/policy.ts +245 -0
  76. package/src/polls-store-memory.ts +32 -0
  77. package/src/polls.ts +312 -0
  78. package/src/presentation.ts +93 -0
  79. package/src/probe.ts +132 -0
  80. package/src/reply-dispatcher.ts +523 -0
  81. package/src/reply-stream-controller.ts +334 -0
  82. package/src/resolve-allowlist.ts +309 -0
  83. package/src/revoked-context.ts +17 -0
  84. package/src/runtime.ts +12 -0
  85. package/src/sdk-types.ts +59 -0
  86. package/src/sdk.ts +916 -0
  87. package/src/secret-contract.ts +49 -0
  88. package/src/secret-input.ts +7 -0
  89. package/src/send-context.ts +269 -0
  90. package/src/send.ts +697 -0
  91. package/src/sent-message-cache.ts +174 -0
  92. package/src/session-route.ts +40 -0
  93. package/src/setup-core.ts +162 -0
  94. package/src/setup-surface.ts +319 -0
  95. package/src/sso-token-store.ts +166 -0
  96. package/src/sso.ts +300 -0
  97. package/src/storage.ts +25 -0
  98. package/src/store-fs.ts +42 -0
  99. package/src/streaming-message.ts +327 -0
  100. package/src/thread-parent-context.ts +159 -0
  101. package/src/token-response.ts +11 -0
  102. package/src/token.ts +194 -0
  103. package/src/user-agent.ts +53 -0
  104. package/src/webhook-timeouts.ts +27 -0
  105. package/src/welcome-card.ts +57 -0
  106. package/test-api.ts +1 -0
  107. package/tsconfig.json +16 -0
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Filesystem-backed pending upload store for the FileConsentCard flow.
3
+ *
4
+ * The CLI `message send --media` path runs in a different process from the
5
+ * gateway's bot monitor that receives the `fileConsent/invoke` callback.
6
+ * An in-memory `pending-uploads.ts` store cannot bridge those processes, so
7
+ * when the user clicks "Allow" the monitor handler's lookup misses and the
8
+ * user sees "card action not supported".
9
+ *
10
+ * This FS store persists pending uploads to a JSON file (with the file buffer
11
+ * base64-encoded) so any process that shares the AutoBot state dir can read
12
+ * them back. The in-memory store in `pending-uploads.ts` is still the fast
13
+ * path for same-process flows (for example the messenger reply path); this FS
14
+ * store is a cross-process fallback.
15
+ */
16
+
17
+ import { resolveMSTeamsStorePath } from "./storage.js";
18
+ import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
19
+
20
+ /** TTL for persisted pending uploads (matches in-memory store). */
21
+ const PENDING_UPLOAD_TTL_MS = 5 * 60 * 1000;
22
+
23
+ /** Cap to avoid unbounded growth if a process crashes mid-flow. */
24
+ const MAX_PENDING_UPLOADS = 100;
25
+
26
+ const STORE_FILENAME = "msteams-pending-uploads.json";
27
+
28
+ type PendingUploadFsRecord = {
29
+ id: string;
30
+ bufferBase64: string;
31
+ filename: string;
32
+ contentType?: string;
33
+ conversationId: string;
34
+ /** Activity ID of the original FileConsentCard, used to replace it after upload */
35
+ consentCardActivityId?: string;
36
+ createdAt: number;
37
+ };
38
+
39
+ type PendingUploadFs = {
40
+ id: string;
41
+ buffer: Buffer;
42
+ filename: string;
43
+ contentType?: string;
44
+ conversationId: string;
45
+ consentCardActivityId?: string;
46
+ createdAt: number;
47
+ };
48
+
49
+ type PendingUploadStoreData = {
50
+ version: 1;
51
+ uploads: Record<string, PendingUploadFsRecord>;
52
+ };
53
+
54
+ const empty: PendingUploadStoreData = { version: 1, uploads: {} };
55
+
56
+ type PendingUploadsFsOptions = {
57
+ env?: NodeJS.ProcessEnv;
58
+ homedir?: () => string;
59
+ stateDir?: string;
60
+ storePath?: string;
61
+ ttlMs?: number;
62
+ };
63
+
64
+ function resolveFilePath(options: PendingUploadsFsOptions | undefined): string {
65
+ return resolveMSTeamsStorePath({
66
+ filename: STORE_FILENAME,
67
+ env: options?.env,
68
+ homedir: options?.homedir,
69
+ stateDir: options?.stateDir,
70
+ storePath: options?.storePath,
71
+ });
72
+ }
73
+
74
+ function pruneExpired(
75
+ uploads: Record<string, PendingUploadFsRecord>,
76
+ nowMs: number,
77
+ ttlMs: number,
78
+ ): Record<string, PendingUploadFsRecord> {
79
+ const kept: Record<string, PendingUploadFsRecord> = {};
80
+ for (const [id, record] of Object.entries(uploads)) {
81
+ if (nowMs - record.createdAt <= ttlMs) {
82
+ kept[id] = record;
83
+ }
84
+ }
85
+ return kept;
86
+ }
87
+
88
+ function pruneToLimit(
89
+ uploads: Record<string, PendingUploadFsRecord>,
90
+ ): Record<string, PendingUploadFsRecord> {
91
+ const entries = Object.entries(uploads);
92
+ if (entries.length <= MAX_PENDING_UPLOADS) {
93
+ return uploads;
94
+ }
95
+ // Oldest createdAt first; drop the oldest until we fit.
96
+ entries.sort((a, b) => a[1].createdAt - b[1].createdAt);
97
+ const keep = entries.slice(entries.length - MAX_PENDING_UPLOADS);
98
+ return Object.fromEntries(keep);
99
+ }
100
+
101
+ function recordToUpload(record: PendingUploadFsRecord): PendingUploadFs {
102
+ return {
103
+ id: record.id,
104
+ buffer: Buffer.from(record.bufferBase64, "base64"),
105
+ filename: record.filename,
106
+ contentType: record.contentType,
107
+ conversationId: record.conversationId,
108
+ consentCardActivityId: record.consentCardActivityId,
109
+ createdAt: record.createdAt,
110
+ };
111
+ }
112
+
113
+ function isValidStore(value: unknown): value is PendingUploadStoreData {
114
+ if (!value || typeof value !== "object") {
115
+ return false;
116
+ }
117
+ const candidate = value as Partial<PendingUploadStoreData>;
118
+ return (
119
+ candidate.version === 1 &&
120
+ typeof candidate.uploads === "object" &&
121
+ candidate.uploads !== null &&
122
+ !Array.isArray(candidate.uploads)
123
+ );
124
+ }
125
+
126
+ async function readStore(filePath: string, ttlMs: number): Promise<PendingUploadStoreData> {
127
+ const { value } = await readJsonFile<unknown>(filePath, empty);
128
+ if (!isValidStore(value)) {
129
+ return { version: 1, uploads: {} };
130
+ }
131
+ const uploads = pruneToLimit(pruneExpired(value.uploads, Date.now(), ttlMs));
132
+ return { version: 1, uploads };
133
+ }
134
+
135
+ /**
136
+ * Persist a pending upload record so another process can read it back.
137
+ * Pass in the pre-generated id (same as the one placed in the consent card
138
+ * context) so the in-memory and FS stores share the same key.
139
+ */
140
+ export async function storePendingUploadFs(
141
+ upload: {
142
+ id: string;
143
+ buffer: Buffer;
144
+ filename: string;
145
+ contentType?: string;
146
+ conversationId: string;
147
+ consentCardActivityId?: string;
148
+ },
149
+ options?: PendingUploadsFsOptions,
150
+ ): Promise<void> {
151
+ const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
152
+ const filePath = resolveFilePath(options);
153
+ await withFileLock(filePath, empty, async () => {
154
+ const store = await readStore(filePath, ttlMs);
155
+ store.uploads[upload.id] = {
156
+ id: upload.id,
157
+ bufferBase64: upload.buffer.toString("base64"),
158
+ filename: upload.filename,
159
+ contentType: upload.contentType,
160
+ conversationId: upload.conversationId,
161
+ consentCardActivityId: upload.consentCardActivityId,
162
+ createdAt: Date.now(),
163
+ };
164
+ store.uploads = pruneToLimit(pruneExpired(store.uploads, Date.now(), ttlMs));
165
+ await writeJsonFile(filePath, store);
166
+ });
167
+ }
168
+
169
+ /**
170
+ * Retrieve a persisted pending upload. Expired entries are treated as absent.
171
+ */
172
+ export async function getPendingUploadFs(
173
+ id: string | undefined,
174
+ options?: PendingUploadsFsOptions,
175
+ ): Promise<PendingUploadFs | undefined> {
176
+ if (!id) {
177
+ return undefined;
178
+ }
179
+ const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
180
+ const filePath = resolveFilePath(options);
181
+ const store = await readStore(filePath, ttlMs);
182
+ const record = store.uploads[id];
183
+ if (!record) {
184
+ return undefined;
185
+ }
186
+ if (Date.now() - record.createdAt > ttlMs) {
187
+ return undefined;
188
+ }
189
+ return recordToUpload(record);
190
+ }
191
+
192
+ /**
193
+ * Remove a persisted pending upload (after successful upload or decline).
194
+ * No-op if the entry is already gone.
195
+ */
196
+ export async function removePendingUploadFs(
197
+ id: string | undefined,
198
+ options?: PendingUploadsFsOptions,
199
+ ): Promise<void> {
200
+ if (!id) {
201
+ return;
202
+ }
203
+ const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
204
+ const filePath = resolveFilePath(options);
205
+ await withFileLock(filePath, empty, async () => {
206
+ const store = await readStore(filePath, ttlMs);
207
+ if (!(id in store.uploads)) {
208
+ return;
209
+ }
210
+ delete store.uploads[id];
211
+ await writeJsonFile(filePath, store);
212
+ });
213
+ }
214
+
215
+ /**
216
+ * Set the consent card activity ID on a persisted entry. Called after the
217
+ * FileConsentCard activity is sent and we know its message id.
218
+ */
219
+ export async function setPendingUploadActivityIdFs(
220
+ id: string,
221
+ activityId: string,
222
+ options?: PendingUploadsFsOptions,
223
+ ): Promise<void> {
224
+ const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
225
+ const filePath = resolveFilePath(options);
226
+ await withFileLock(filePath, empty, async () => {
227
+ const store = await readStore(filePath, ttlMs);
228
+ const record = store.uploads[id];
229
+ if (!record) {
230
+ return;
231
+ }
232
+ record.consentCardActivityId = activityId;
233
+ await writeJsonFile(filePath, store);
234
+ });
235
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * In-memory storage for files awaiting user consent in the FileConsentCard flow.
3
+ *
4
+ * When sending large files (>=4MB) in personal chats, Teams requires user consent
5
+ * before upload. This module stores the file data temporarily until the user
6
+ * accepts or declines, or until the TTL expires.
7
+ */
8
+
9
+ import crypto from "node:crypto";
10
+
11
+ export interface PendingUpload {
12
+ id: string;
13
+ buffer: Buffer;
14
+ filename: string;
15
+ contentType?: string;
16
+ conversationId: string;
17
+ /** Activity ID of the original FileConsentCard, used to replace it after upload */
18
+ consentCardActivityId?: string;
19
+ createdAt: number;
20
+ }
21
+
22
+ const pendingUploads = new Map<string, PendingUpload>();
23
+ /** Timer handles keyed by upload ID, cleared on explicit removal to prevent ghost cleanup */
24
+ const pendingUploadTimers = new Map<string, ReturnType<typeof setTimeout>>();
25
+
26
+ /** TTL for pending uploads: 5 minutes */
27
+ const PENDING_UPLOAD_TTL_MS = 5 * 60 * 1000;
28
+
29
+ /**
30
+ * Store a file pending user consent.
31
+ * Returns the upload ID to include in the FileConsentCard context.
32
+ */
33
+ export function storePendingUpload(upload: Omit<PendingUpload, "id" | "createdAt">): string {
34
+ const id = crypto.randomUUID();
35
+ const entry: PendingUpload = {
36
+ ...upload,
37
+ id,
38
+ createdAt: Date.now(),
39
+ };
40
+ pendingUploads.set(id, entry);
41
+
42
+ // Auto-cleanup after TTL; timer ref stored so removePendingUpload can cancel it
43
+ const timer = setTimeout(() => {
44
+ pendingUploads.delete(id);
45
+ pendingUploadTimers.delete(id);
46
+ }, PENDING_UPLOAD_TTL_MS);
47
+ pendingUploadTimers.set(id, timer);
48
+
49
+ return id;
50
+ }
51
+
52
+ /**
53
+ * Retrieve a pending upload by ID.
54
+ * Returns undefined if not found or expired.
55
+ */
56
+ export function getPendingUpload(id?: string): PendingUpload | undefined {
57
+ if (!id) {
58
+ return undefined;
59
+ }
60
+ const entry = pendingUploads.get(id);
61
+ if (!entry) {
62
+ return undefined;
63
+ }
64
+
65
+ // Check if expired (in case timeout hasn't fired yet)
66
+ if (Date.now() - entry.createdAt > PENDING_UPLOAD_TTL_MS) {
67
+ pendingUploads.delete(id);
68
+ const timer = pendingUploadTimers.get(id);
69
+ if (timer !== undefined) {
70
+ clearTimeout(timer);
71
+ pendingUploadTimers.delete(id);
72
+ }
73
+ return undefined;
74
+ }
75
+
76
+ return entry;
77
+ }
78
+
79
+ /**
80
+ * Remove a pending upload (after successful upload or user decline).
81
+ * Also clears the TTL timer to prevent ghost Map deletions.
82
+ */
83
+ export function removePendingUpload(id?: string): void {
84
+ if (id) {
85
+ pendingUploads.delete(id);
86
+ const timer = pendingUploadTimers.get(id);
87
+ if (timer !== undefined) {
88
+ clearTimeout(timer);
89
+ pendingUploadTimers.delete(id);
90
+ }
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Set the consent card activity ID on an existing pending upload.
96
+ * Called after the FileConsentCard is sent and we know its activity ID.
97
+ */
98
+ export function setPendingUploadActivityId(uploadId: string, activityId: string): void {
99
+ const entry = pendingUploads.get(uploadId);
100
+ if (entry) {
101
+ entry.consentCardActivityId = activityId;
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Get the count of pending uploads (for monitoring/debugging).
107
+ */
108
+ export function getPendingUploadCount(): number {
109
+ return pendingUploads.size;
110
+ }
111
+
112
+ /**
113
+ * Clear all pending uploads (for testing).
114
+ */
115
+ export function clearPendingUploads(): void {
116
+ for (const timer of pendingUploadTimers.values()) {
117
+ clearTimeout(timer);
118
+ }
119
+ pendingUploadTimers.clear();
120
+ pendingUploads.clear();
121
+ }
package/src/policy.ts ADDED
@@ -0,0 +1,245 @@
1
+ import type {
2
+ AllowlistMatch,
3
+ ChannelGroupContext,
4
+ GroupToolPolicyConfig,
5
+ MSTeamsChannelConfig,
6
+ MSTeamsConfig,
7
+ MSTeamsReplyStyle,
8
+ MSTeamsTeamConfig,
9
+ } from "../runtime-api.js";
10
+ import {
11
+ buildChannelKeyCandidates,
12
+ normalizeChannelSlug,
13
+ resolveAllowlistMatchSimple,
14
+ resolveToolsBySender,
15
+ resolveChannelEntryMatchWithFallback,
16
+ resolveNestedAllowlistDecision,
17
+ isDangerousNameMatchingEnabled,
18
+ } from "../runtime-api.js";
19
+
20
+ type MSTeamsResolvedRouteConfig = {
21
+ teamConfig?: MSTeamsTeamConfig;
22
+ channelConfig?: MSTeamsChannelConfig;
23
+ allowlistConfigured: boolean;
24
+ allowed: boolean;
25
+ teamKey?: string;
26
+ channelKey?: string;
27
+ channelMatchKey?: string;
28
+ channelMatchSource?: "direct" | "wildcard";
29
+ };
30
+
31
+ export function resolveMSTeamsRouteConfig(params: {
32
+ cfg?: MSTeamsConfig;
33
+ teamId?: string | null | undefined;
34
+ teamName?: string | null | undefined;
35
+ conversationId?: string | null | undefined;
36
+ channelName?: string | null | undefined;
37
+ allowNameMatching?: boolean;
38
+ }): MSTeamsResolvedRouteConfig {
39
+ const teamId = params.teamId?.trim();
40
+ const teamName = params.teamName?.trim();
41
+ const conversationId = params.conversationId?.trim();
42
+ const channelName = params.channelName?.trim();
43
+ const teams = params.cfg?.teams ?? {};
44
+ const allowlistConfigured = Object.keys(teams).length > 0;
45
+ const teamCandidates = buildChannelKeyCandidates(
46
+ teamId,
47
+ params.allowNameMatching ? teamName : undefined,
48
+ params.allowNameMatching && teamName ? normalizeChannelSlug(teamName) : undefined,
49
+ );
50
+ const teamMatch = resolveChannelEntryMatchWithFallback({
51
+ entries: teams,
52
+ keys: teamCandidates,
53
+ wildcardKey: "*",
54
+ normalizeKey: normalizeChannelSlug,
55
+ });
56
+ const teamConfig = teamMatch.entry;
57
+ const channels = teamConfig?.channels ?? {};
58
+ const channelAllowlistConfigured = Object.keys(channels).length > 0;
59
+ const channelCandidates = buildChannelKeyCandidates(
60
+ conversationId,
61
+ params.allowNameMatching ? channelName : undefined,
62
+ params.allowNameMatching && channelName ? normalizeChannelSlug(channelName) : undefined,
63
+ );
64
+ const channelMatch = resolveChannelEntryMatchWithFallback({
65
+ entries: channels,
66
+ keys: channelCandidates,
67
+ wildcardKey: "*",
68
+ normalizeKey: normalizeChannelSlug,
69
+ });
70
+ const channelConfig = channelMatch.entry;
71
+
72
+ const allowed = resolveNestedAllowlistDecision({
73
+ outerConfigured: allowlistConfigured,
74
+ outerMatched: Boolean(teamConfig),
75
+ innerConfigured: channelAllowlistConfigured,
76
+ innerMatched: Boolean(channelConfig),
77
+ });
78
+
79
+ return {
80
+ teamConfig,
81
+ channelConfig,
82
+ allowlistConfigured,
83
+ allowed,
84
+ teamKey: teamMatch.matchKey ?? teamMatch.key,
85
+ channelKey: channelMatch.matchKey ?? channelMatch.key,
86
+ channelMatchKey: channelMatch.matchKey,
87
+ channelMatchSource:
88
+ channelMatch.matchSource === "direct" || channelMatch.matchSource === "wildcard"
89
+ ? channelMatch.matchSource
90
+ : undefined,
91
+ };
92
+ }
93
+
94
+ export function resolveMSTeamsGroupToolPolicy(
95
+ params: ChannelGroupContext,
96
+ ): GroupToolPolicyConfig | undefined {
97
+ const cfg = params.cfg.channels?.msteams;
98
+ if (!cfg) {
99
+ return undefined;
100
+ }
101
+ const groupId = params.groupId?.trim();
102
+ const groupChannel = params.groupChannel?.trim();
103
+ const groupSpace = params.groupSpace?.trim();
104
+ const allowNameMatching = isDangerousNameMatchingEnabled(cfg);
105
+
106
+ const resolved = resolveMSTeamsRouteConfig({
107
+ cfg,
108
+ teamId: groupSpace,
109
+ teamName: groupSpace,
110
+ conversationId: groupId,
111
+ channelName: groupChannel,
112
+ allowNameMatching,
113
+ });
114
+
115
+ if (resolved.channelConfig) {
116
+ const senderPolicy = resolveToolsBySender({
117
+ toolsBySender: resolved.channelConfig.toolsBySender,
118
+ senderId: params.senderId,
119
+ senderName: params.senderName,
120
+ senderUsername: params.senderUsername,
121
+ senderE164: params.senderE164,
122
+ });
123
+ if (senderPolicy) {
124
+ return senderPolicy;
125
+ }
126
+ if (resolved.channelConfig.tools) {
127
+ return resolved.channelConfig.tools;
128
+ }
129
+ const teamSenderPolicy = resolveToolsBySender({
130
+ toolsBySender: resolved.teamConfig?.toolsBySender,
131
+ senderId: params.senderId,
132
+ senderName: params.senderName,
133
+ senderUsername: params.senderUsername,
134
+ senderE164: params.senderE164,
135
+ });
136
+ if (teamSenderPolicy) {
137
+ return teamSenderPolicy;
138
+ }
139
+ return resolved.teamConfig?.tools;
140
+ }
141
+ if (resolved.teamConfig) {
142
+ const teamSenderPolicy = resolveToolsBySender({
143
+ toolsBySender: resolved.teamConfig.toolsBySender,
144
+ senderId: params.senderId,
145
+ senderName: params.senderName,
146
+ senderUsername: params.senderUsername,
147
+ senderE164: params.senderE164,
148
+ });
149
+ if (teamSenderPolicy) {
150
+ return teamSenderPolicy;
151
+ }
152
+ if (resolved.teamConfig.tools) {
153
+ return resolved.teamConfig.tools;
154
+ }
155
+ }
156
+
157
+ if (!groupId) {
158
+ return undefined;
159
+ }
160
+
161
+ const channelCandidates = buildChannelKeyCandidates(
162
+ groupId,
163
+ allowNameMatching ? groupChannel : undefined,
164
+ allowNameMatching && groupChannel ? normalizeChannelSlug(groupChannel) : undefined,
165
+ );
166
+ for (const teamConfig of Object.values(cfg.teams ?? {})) {
167
+ const match = resolveChannelEntryMatchWithFallback({
168
+ entries: teamConfig?.channels ?? {},
169
+ keys: channelCandidates,
170
+ wildcardKey: "*",
171
+ normalizeKey: normalizeChannelSlug,
172
+ });
173
+ if (match.entry) {
174
+ const senderPolicy = resolveToolsBySender({
175
+ toolsBySender: match.entry.toolsBySender,
176
+ senderId: params.senderId,
177
+ senderName: params.senderName,
178
+ senderUsername: params.senderUsername,
179
+ senderE164: params.senderE164,
180
+ });
181
+ if (senderPolicy) {
182
+ return senderPolicy;
183
+ }
184
+ if (match.entry.tools) {
185
+ return match.entry.tools;
186
+ }
187
+ const teamSenderPolicy = resolveToolsBySender({
188
+ toolsBySender: teamConfig?.toolsBySender,
189
+ senderId: params.senderId,
190
+ senderName: params.senderName,
191
+ senderUsername: params.senderUsername,
192
+ senderE164: params.senderE164,
193
+ });
194
+ if (teamSenderPolicy) {
195
+ return teamSenderPolicy;
196
+ }
197
+ return teamConfig?.tools;
198
+ }
199
+ }
200
+
201
+ return undefined;
202
+ }
203
+
204
+ type MSTeamsReplyPolicy = {
205
+ requireMention: boolean;
206
+ replyStyle: MSTeamsReplyStyle;
207
+ };
208
+
209
+ type MSTeamsAllowlistMatch = AllowlistMatch<"wildcard" | "id" | "name">;
210
+
211
+ export function resolveMSTeamsAllowlistMatch(params: {
212
+ allowFrom: Array<string | number>;
213
+ senderId: string;
214
+ senderName?: string | null;
215
+ allowNameMatching?: boolean;
216
+ }): MSTeamsAllowlistMatch {
217
+ return resolveAllowlistMatchSimple(params);
218
+ }
219
+
220
+ export function resolveMSTeamsReplyPolicy(params: {
221
+ isDirectMessage: boolean;
222
+ globalConfig?: MSTeamsConfig;
223
+ teamConfig?: MSTeamsTeamConfig;
224
+ channelConfig?: MSTeamsChannelConfig;
225
+ }): MSTeamsReplyPolicy {
226
+ if (params.isDirectMessage) {
227
+ return { requireMention: false, replyStyle: "thread" };
228
+ }
229
+
230
+ const requireMention =
231
+ params.channelConfig?.requireMention ??
232
+ params.teamConfig?.requireMention ??
233
+ params.globalConfig?.requireMention ??
234
+ true;
235
+
236
+ const explicitReplyStyle =
237
+ params.channelConfig?.replyStyle ??
238
+ params.teamConfig?.replyStyle ??
239
+ params.globalConfig?.replyStyle;
240
+
241
+ const replyStyle: MSTeamsReplyStyle =
242
+ explicitReplyStyle ?? (requireMention ? "thread" : "top-level");
243
+
244
+ return { requireMention, replyStyle };
245
+ }
@@ -0,0 +1,32 @@
1
+ import {
2
+ type MSTeamsPoll,
3
+ type MSTeamsPollStore,
4
+ normalizeMSTeamsPollSelections,
5
+ } from "./polls.js";
6
+
7
+ export function createMSTeamsPollStoreMemory(initial: MSTeamsPoll[] = []): MSTeamsPollStore {
8
+ const polls = new Map<string, MSTeamsPoll>();
9
+ for (const poll of initial) {
10
+ polls.set(poll.id, { ...poll });
11
+ }
12
+
13
+ const createPoll = async (poll: MSTeamsPoll) => {
14
+ polls.set(poll.id, { ...poll });
15
+ };
16
+
17
+ const getPoll = async (pollId: string) => polls.get(pollId) ?? null;
18
+
19
+ const recordVote = async (params: { pollId: string; voterId: string; selections: string[] }) => {
20
+ const poll = polls.get(params.pollId);
21
+ if (!poll) {
22
+ return null;
23
+ }
24
+ const normalized = normalizeMSTeamsPollSelections(poll, params.selections);
25
+ poll.votes[params.voterId] = normalized;
26
+ poll.updatedAt = new Date().toISOString();
27
+ polls.set(poll.id, poll);
28
+ return poll;
29
+ };
30
+
31
+ return { createPoll, getPoll, recordVote };
32
+ }