@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,49 @@
1
+ import {
2
+ collectSecretInputAssignment,
3
+ getChannelRecord,
4
+ type ResolverContext,
5
+ type SecretDefaults,
6
+ } from "autobot/plugin-sdk/channel-secret-basic-runtime";
7
+
8
+ export const secretTargetRegistryEntries: import("autobot/plugin-sdk/channel-secret-basic-runtime").SecretTargetRegistryEntry[] =
9
+ [
10
+ {
11
+ id: "channels.msteams.appPassword",
12
+ targetType: "channels.msteams.appPassword",
13
+ configFile: "autobot.json",
14
+ pathPattern: "channels.msteams.appPassword",
15
+ secretShape: "secret_input",
16
+ expectedResolvedValue: "string",
17
+ includeInPlan: true,
18
+ includeInConfigure: true,
19
+ includeInAudit: true,
20
+ },
21
+ ];
22
+
23
+ export function collectRuntimeConfigAssignments(params: {
24
+ config: { channels?: Record<string, unknown> };
25
+ defaults?: SecretDefaults;
26
+ context: ResolverContext;
27
+ }): void {
28
+ const msteams = getChannelRecord(params.config, "msteams");
29
+ if (!msteams) {
30
+ return;
31
+ }
32
+ collectSecretInputAssignment({
33
+ value: msteams.appPassword,
34
+ path: "channels.msteams.appPassword",
35
+ expected: "string",
36
+ defaults: params.defaults,
37
+ context: params.context,
38
+ active: msteams.enabled !== false,
39
+ inactiveReason: "Microsoft Teams channel is disabled.",
40
+ apply: (value) => {
41
+ msteams.appPassword = value;
42
+ },
43
+ });
44
+ }
45
+
46
+ export const channelSecrets = {
47
+ secretTargetRegistryEntries,
48
+ collectRuntimeConfigAssignments,
49
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ hasConfiguredSecretInput,
3
+ normalizeResolvedSecretInputString,
4
+ normalizeSecretInputString,
5
+ } from "autobot/plugin-sdk/secret-input";
6
+
7
+ export { hasConfiguredSecretInput, normalizeResolvedSecretInputString, normalizeSecretInputString };
@@ -0,0 +1,269 @@
1
+ import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
2
+ import {
3
+ resolveChannelMediaMaxBytes,
4
+ type MSTeamsConfig,
5
+ type MSTeamsReplyStyle,
6
+ type AutoBotConfig,
7
+ type PluginRuntime,
8
+ } from "../runtime-api.js";
9
+ import type { MSTeamsAccessTokenProvider } from "./attachments/types.js";
10
+ import { createMSTeamsConversationStoreFs } from "./conversation-store-fs.js";
11
+ import type {
12
+ MSTeamsConversationStore,
13
+ StoredConversationReference,
14
+ } from "./conversation-store.js";
15
+ import { formatUnknownError } from "./errors.js";
16
+ import { resolveGraphChatId } from "./graph-upload.js";
17
+ import type { MSTeamsAdapter } from "./messenger.js";
18
+ import { resolveMSTeamsReplyPolicy, resolveMSTeamsRouteConfig } from "./policy.js";
19
+ import { getMSTeamsRuntime } from "./runtime.js";
20
+ import { createMSTeamsAdapter, createMSTeamsTokenProvider, loadMSTeamsSdkWithAuth } from "./sdk.js";
21
+ import { resolveMSTeamsCredentials } from "./token.js";
22
+
23
+ type MSTeamsConversationType = "personal" | "groupChat" | "channel";
24
+
25
+ export type MSTeamsProactiveContext = {
26
+ appId: string;
27
+ conversationId: string;
28
+ ref: StoredConversationReference;
29
+ adapter: MSTeamsAdapter;
30
+ log: ReturnType<PluginRuntime["logging"]["getChildLogger"]>;
31
+ /** The type of conversation: personal (1:1), groupChat, or channel */
32
+ conversationType: MSTeamsConversationType;
33
+ /** Reply style resolved for proactive text/media sends. */
34
+ replyStyle: MSTeamsReplyStyle;
35
+ /** Token provider for Graph API / OneDrive operations */
36
+ tokenProvider: MSTeamsAccessTokenProvider;
37
+ /** SharePoint site ID for file uploads in group chats/channels */
38
+ sharePointSiteId?: string;
39
+ /** Resolved media max bytes from config (default: 100MB) */
40
+ mediaMaxBytes?: number;
41
+ /**
42
+ * Graph API-native chat ID for this conversation.
43
+ * Bot Framework personal DM IDs (`a:1xxx` / `8:orgid:xxx`) cannot be used directly
44
+ * with Graph chat endpoints. This field holds the resolved `19:xxx` format ID.
45
+ * Null if resolution failed or not applicable.
46
+ */
47
+ graphChatId?: string | null;
48
+ };
49
+
50
+ export function resolveMSTeamsProactiveReplyStyle(params: {
51
+ cfg?: MSTeamsConfig;
52
+ conversationId: string;
53
+ ref: StoredConversationReference;
54
+ conversationType: MSTeamsConversationType;
55
+ }): MSTeamsReplyStyle {
56
+ const threadRootId = params.ref.threadId ?? params.ref.activityId;
57
+ if (params.conversationType !== "channel" || !threadRootId) {
58
+ return "top-level";
59
+ }
60
+
61
+ const routeConfig = resolveMSTeamsRouteConfig({
62
+ cfg: params.cfg,
63
+ teamId: params.ref.teamId,
64
+ conversationId: params.conversationId,
65
+ allowNameMatching: false,
66
+ });
67
+ const { replyStyle } = resolveMSTeamsReplyPolicy({
68
+ isDirectMessage: false,
69
+ globalConfig: params.cfg,
70
+ teamConfig: routeConfig.teamConfig,
71
+ channelConfig: routeConfig.channelConfig,
72
+ });
73
+ return replyStyle;
74
+ }
75
+
76
+ /**
77
+ * Parse the target value into a conversation reference lookup key.
78
+ * Supported formats:
79
+ * - conversation:19:abc@thread.tacv2 → lookup by conversation ID
80
+ * - user:aad-object-id → lookup by user AAD object ID
81
+ * - 19:abc@thread.tacv2 → direct conversation ID
82
+ */
83
+ function parseRecipient(to: string): {
84
+ type: "conversation" | "user";
85
+ id: string;
86
+ } {
87
+ const trimmed = to.trim();
88
+ const finalize = (type: "conversation" | "user", id: string) => {
89
+ const normalized = id.trim();
90
+ if (!normalized) {
91
+ throw new Error(`Invalid target value: missing ${type} id`);
92
+ }
93
+ return { type, id: normalized };
94
+ };
95
+ if (trimmed.startsWith("conversation:")) {
96
+ return finalize("conversation", trimmed.slice("conversation:".length));
97
+ }
98
+ if (trimmed.startsWith("user:")) {
99
+ return finalize("user", trimmed.slice("user:".length));
100
+ }
101
+ // Assume it's a conversation ID if it looks like one
102
+ if (trimmed.startsWith("19:") || trimmed.includes("@thread")) {
103
+ return finalize("conversation", trimmed);
104
+ }
105
+ // Otherwise treat as user ID
106
+ return finalize("user", trimmed);
107
+ }
108
+
109
+ /**
110
+ * Find a stored conversation reference for the given recipient.
111
+ */
112
+ async function findConversationReference(recipient: {
113
+ type: "conversation" | "user";
114
+ id: string;
115
+ store: MSTeamsConversationStore;
116
+ }): Promise<{
117
+ conversationId: string;
118
+ ref: StoredConversationReference;
119
+ } | null> {
120
+ if (recipient.type === "conversation") {
121
+ const ref = await recipient.store.get(recipient.id);
122
+ if (ref) {
123
+ return { conversationId: recipient.id, ref };
124
+ }
125
+ return null;
126
+ }
127
+
128
+ const found = await recipient.store.findPreferredDmByUserId(recipient.id);
129
+ if (!found) {
130
+ return null;
131
+ }
132
+ return { conversationId: found.conversationId, ref: found.reference };
133
+ }
134
+
135
+ export async function resolveMSTeamsSendContext(params: {
136
+ cfg: AutoBotConfig;
137
+ to: string;
138
+ }): Promise<MSTeamsProactiveContext> {
139
+ const msteamsCfg = params.cfg.channels?.msteams;
140
+
141
+ if (!msteamsCfg?.enabled) {
142
+ throw new Error("msteams provider is not enabled");
143
+ }
144
+
145
+ const creds = resolveMSTeamsCredentials(msteamsCfg);
146
+ if (!creds) {
147
+ throw new Error("msteams credentials not configured");
148
+ }
149
+
150
+ const store = createMSTeamsConversationStoreFs();
151
+
152
+ // Parse recipient and find conversation reference
153
+ const recipient = parseRecipient(params.to);
154
+ const found = await findConversationReference({ ...recipient, store });
155
+
156
+ if (!found) {
157
+ throw new Error(
158
+ `No conversation reference found for ${recipient.type}:${recipient.id}. ` +
159
+ `The bot must receive a message from this conversation before it can send proactively.`,
160
+ );
161
+ }
162
+
163
+ const { conversationId, ref } = found;
164
+
165
+ // Safety check: when the caller targeted a specific user (DM), verify the
166
+ // resolved conversation is actually a personal DM. Without this guard a
167
+ // stale or mismatched conversation store could route a private DM reply
168
+ // into a shared channel or group chat -- see #54520.
169
+ if (recipient.type === "user") {
170
+ const resolvedType = normalizeLowercaseStringOrEmpty(ref.conversation?.conversationType ?? "");
171
+ if (resolvedType && resolvedType !== "personal") {
172
+ throw new Error(
173
+ `Conversation reference for user:${recipient.id} resolved to a ${resolvedType} ` +
174
+ `conversation (${conversationId}) instead of a personal DM. ` +
175
+ `The bot must receive a DM from this user before it can send proactively.`,
176
+ );
177
+ }
178
+ }
179
+ const core = getMSTeamsRuntime();
180
+ const log = core.logging.getChildLogger({ name: "msteams:send" });
181
+
182
+ const { sdk, app } = await loadMSTeamsSdkWithAuth(creds);
183
+ const adapter = createMSTeamsAdapter(app, sdk);
184
+
185
+ // Create token provider adapter for Graph API / OneDrive operations
186
+ const tokenProvider: MSTeamsAccessTokenProvider = createMSTeamsTokenProvider(app);
187
+
188
+ // Determine conversation type from stored reference
189
+ const storedConversationType = normalizeLowercaseStringOrEmpty(
190
+ ref.conversation?.conversationType ?? "",
191
+ );
192
+ let conversationType: MSTeamsConversationType;
193
+ if (storedConversationType === "personal") {
194
+ conversationType = "personal";
195
+ } else if (storedConversationType === "channel") {
196
+ conversationType = "channel";
197
+ } else {
198
+ // groupChat, or unknown defaults to groupChat behavior
199
+ conversationType = "groupChat";
200
+ }
201
+ const replyStyle = resolveMSTeamsProactiveReplyStyle({
202
+ cfg: msteamsCfg,
203
+ conversationId,
204
+ ref,
205
+ conversationType,
206
+ });
207
+
208
+ // Get SharePoint site ID from config (required for file uploads in group chats/channels)
209
+ const sharePointSiteId = msteamsCfg.sharePointSiteId;
210
+
211
+ // Resolve media max bytes from config
212
+ const mediaMaxBytes = resolveChannelMediaMaxBytes({
213
+ cfg: params.cfg,
214
+ resolveChannelLimitMb: ({ cfg }) => cfg.channels?.msteams?.mediaMaxMb,
215
+ });
216
+
217
+ // Resolve Graph API-native chat ID if needed for SharePoint per-user sharing.
218
+ // Bot Framework personal DM conversation IDs (e.g. `a:1xxx` or `8:orgid:xxx`) cannot
219
+ // be used directly with Graph /chats/{chatId} endpoints — the Graph API requires the
220
+ // `19:xxx@thread.tacv2` or `19:xxx@unq.gbl.spaces` format.
221
+ // We check the cached value first, then resolve via Graph API and cache for future sends.
222
+ let graphChatId: string | null | undefined = ref.graphChatId ?? undefined;
223
+ if (graphChatId === undefined && sharePointSiteId) {
224
+ // Only resolve when SharePoint is configured (the only place chatId matters currently)
225
+ try {
226
+ const resolved = await resolveGraphChatId({
227
+ botFrameworkConversationId: conversationId,
228
+ userAadObjectId: ref.user?.aadObjectId,
229
+ tokenProvider,
230
+ });
231
+ graphChatId = resolved;
232
+
233
+ // Cache in the conversation store so subsequent sends skip the Graph lookup.
234
+ // NOTE: We intentionally do NOT cache null results. Transient Graph API failures
235
+ // (network, 401, rate limit) should be retried on subsequent sends rather than
236
+ // permanently blocking file uploads for this conversation.
237
+ if (resolved) {
238
+ await store.upsert(conversationId, { ...ref, graphChatId: resolved });
239
+ } else {
240
+ log.warn?.("could not resolve Graph chat ID; file uploads may fail for this conversation", {
241
+ conversationId,
242
+ });
243
+ }
244
+ } catch (err) {
245
+ log.warn?.(
246
+ "failed to resolve Graph chat ID; file uploads may fall back to Bot Framework ID",
247
+ {
248
+ conversationId,
249
+ error: formatUnknownError(err),
250
+ },
251
+ );
252
+ graphChatId = null;
253
+ }
254
+ }
255
+
256
+ return {
257
+ appId: creds.appId,
258
+ conversationId,
259
+ ref,
260
+ adapter: adapter as unknown as MSTeamsAdapter,
261
+ log,
262
+ conversationType,
263
+ replyStyle,
264
+ tokenProvider,
265
+ sharePointSiteId,
266
+ mediaMaxBytes,
267
+ graphChatId,
268
+ };
269
+ }