@kodelyth/msteams 2026.5.42 → 2026.6.2

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 (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +18 -6
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
@@ -1,325 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import type { KlawConfig, PluginRuntime } from "../../runtime-api.js";
3
- import type { MSTeamsMessageHandlerDeps } from "../monitor-handler.js";
4
- import { setMSTeamsRuntime } from "../runtime.js";
5
- import { createMSTeamsReactionHandler } from "./reaction-handler.js";
6
-
7
- function buildMockRuntime(overrides?: Partial<PluginRuntime>): PluginRuntime {
8
- return {
9
- logging: { shouldLogVerbose: () => false },
10
- channel: {
11
- routing: {
12
- resolveAgentRoute: vi.fn(() => ({
13
- sessionKey: "test-session",
14
- agentId: "agent1",
15
- accountId: "default",
16
- })),
17
- },
18
- pairing: {
19
- readAllowFromStore: vi.fn(async () => []),
20
- upsertPairingRequest: vi.fn(async () => null),
21
- },
22
- },
23
- system: {
24
- enqueueSystemEvent: vi.fn(),
25
- },
26
- ...overrides,
27
- } as unknown as PluginRuntime;
28
- }
29
-
30
- function buildDeps(cfg: KlawConfig, _runtime?: PluginRuntime): MSTeamsMessageHandlerDeps {
31
- return {
32
- cfg,
33
- runtime: { error: vi.fn() } as unknown as MSTeamsMessageHandlerDeps["runtime"],
34
- appId: "test-app",
35
- adapter: {} as MSTeamsMessageHandlerDeps["adapter"],
36
- tokenProvider: { getAccessToken: vi.fn(async () => "token") },
37
- textLimit: 4000,
38
- mediaMaxBytes: 1024 * 1024,
39
- conversationStore: {
40
- upsert: vi.fn(async () => undefined),
41
- } as unknown as MSTeamsMessageHandlerDeps["conversationStore"],
42
- pollStore: {
43
- recordVote: vi.fn(async () => null),
44
- } as unknown as MSTeamsMessageHandlerDeps["pollStore"],
45
- log: {
46
- info: vi.fn(),
47
- debug: vi.fn(),
48
- error: vi.fn(),
49
- warn: vi.fn(),
50
- } as unknown as MSTeamsMessageHandlerDeps["log"],
51
- };
52
- }
53
-
54
- function createReactionTestHarness() {
55
- const mockRuntime = buildMockRuntime();
56
- setMSTeamsRuntime(mockRuntime);
57
-
58
- const cfg: KlawConfig = {
59
- channels: { msteams: { allowFrom: ["allowed-aad"] } },
60
- } as KlawConfig;
61
-
62
- const deps = buildDeps(cfg, mockRuntime);
63
- const handler = createMSTeamsReactionHandler(deps);
64
- const enqueue = mockRuntime.system.enqueueSystemEvent as ReturnType<typeof vi.fn>;
65
-
66
- return { handler, enqueue };
67
- }
68
-
69
- function firstEnqueueCall(enqueue: ReturnType<typeof vi.fn>): unknown[] {
70
- const [call] = enqueue.mock.calls;
71
- if (!call) {
72
- throw new Error("Expected enqueueSystemEvent call");
73
- }
74
- return call;
75
- }
76
-
77
- function firstEnqueueLabel(enqueue: ReturnType<typeof vi.fn>): string {
78
- const [label] = firstEnqueueCall(enqueue);
79
- if (typeof label !== "string") {
80
- throw new Error("Expected enqueueSystemEvent label");
81
- }
82
- return label;
83
- }
84
-
85
- function firstEnqueueMeta(enqueue: ReturnType<typeof vi.fn>): Record<string, unknown> {
86
- const [, meta] = firstEnqueueCall(enqueue);
87
- if (!meta || typeof meta !== "object") {
88
- throw new Error("Expected enqueueSystemEvent metadata");
89
- }
90
- return meta as Record<string, unknown>;
91
- }
92
-
93
- async function invokeReactionEvent(
94
- handler: ReturnType<typeof createMSTeamsReactionHandler>,
95
- activity: Record<string, unknown>,
96
- direction: "added" | "removed",
97
- ) {
98
- await handler(
99
- {
100
- activity: {
101
- type: "messageReaction",
102
- conversation: { id: "dm-conv", conversationType: "personal" },
103
- ...activity,
104
- },
105
- sendActivity: vi.fn(async () => undefined),
106
- } as never,
107
- direction,
108
- );
109
- }
110
-
111
- describe("createMSTeamsReactionHandler", () => {
112
- describe("emoji mapping", () => {
113
- it("maps Teams reaction types to unicode emoji in event label", async () => {
114
- const mockRuntime = buildMockRuntime();
115
- setMSTeamsRuntime(mockRuntime);
116
-
117
- const cfg: KlawConfig = {
118
- channels: {
119
- msteams: {
120
- allowFrom: ["allowed-aad"],
121
- },
122
- },
123
- } as KlawConfig;
124
-
125
- const deps = buildDeps(cfg);
126
- const handler = createMSTeamsReactionHandler(deps);
127
-
128
- await handler(
129
- {
130
- activity: {
131
- type: "messageReaction",
132
- reactionsAdded: [{ type: "like" }],
133
- from: { id: "user-id", aadObjectId: "allowed-aad", name: "Alice" },
134
- conversation: { id: "personal-conv", conversationType: "personal" },
135
- replyToId: "msg-123",
136
- },
137
- sendActivity: vi.fn(async () => undefined),
138
- } as never,
139
- "added",
140
- );
141
-
142
- const enqueue = mockRuntime.system.enqueueSystemEvent as ReturnType<typeof vi.fn>;
143
- expect(enqueue).toHaveBeenCalledOnce();
144
- const label = firstEnqueueLabel(enqueue);
145
- expect(label).toContain("👍");
146
- expect(label).toContain("Alice");
147
- expect(label).toContain("msg-123");
148
- });
149
-
150
- it("maps heart, laugh, surprised, sad, angry reaction types", async () => {
151
- const emojiMap: Record<string, string> = {
152
- heart: "❤️",
153
- laugh: "😆",
154
- surprised: "😮",
155
- sad: "😢",
156
- angry: "😡",
157
- };
158
-
159
- for (const [type, expectedEmoji] of Object.entries(emojiMap)) {
160
- const mockRuntime = buildMockRuntime();
161
- setMSTeamsRuntime(mockRuntime);
162
-
163
- const cfg: KlawConfig = {
164
- channels: { msteams: { allowFrom: ["allowed-aad"] } },
165
- } as KlawConfig;
166
-
167
- const deps = buildDeps(cfg, mockRuntime);
168
- const handler = createMSTeamsReactionHandler(deps);
169
-
170
- await handler(
171
- {
172
- activity: {
173
- type: "messageReaction",
174
- reactionsAdded: [{ type }],
175
- from: { id: "user-id", aadObjectId: "allowed-aad", name: "Bob" },
176
- conversation: { id: "dm-conv", conversationType: "personal" },
177
- replyToId: "msg-456",
178
- },
179
- sendActivity: vi.fn(async () => undefined),
180
- } as never,
181
- "added",
182
- );
183
-
184
- const enqueue = mockRuntime.system.enqueueSystemEvent as ReturnType<typeof vi.fn>;
185
- const label = firstEnqueueLabel(enqueue);
186
- expect(label).toContain(expectedEmoji);
187
- }
188
- });
189
- });
190
-
191
- describe("inbound reaction events", () => {
192
- it("enqueues system event for reactionsAdded", async () => {
193
- const { handler, enqueue } = createReactionTestHarness();
194
- await invokeReactionEvent(
195
- handler,
196
- {
197
- reactionsAdded: [{ type: "like" }],
198
- from: { id: "u1", aadObjectId: "allowed-aad", name: "User" },
199
- replyToId: "msg-1",
200
- },
201
- "added",
202
- );
203
-
204
- expect(enqueue).toHaveBeenCalledOnce();
205
- const label = firstEnqueueLabel(enqueue);
206
- const meta = firstEnqueueMeta(enqueue);
207
- expect(label).toContain("added");
208
- expect(meta.sessionKey).toBe("test-session");
209
- expect(meta.contextKey).toContain("added");
210
- });
211
-
212
- it("enqueues system event for reactionsRemoved", async () => {
213
- const { handler, enqueue } = createReactionTestHarness();
214
- await invokeReactionEvent(
215
- handler,
216
- {
217
- reactionsRemoved: [{ type: "heart" }],
218
- from: { id: "u1", aadObjectId: "allowed-aad", name: "User" },
219
- replyToId: "msg-2",
220
- },
221
- "removed",
222
- );
223
-
224
- expect(enqueue).toHaveBeenCalledOnce();
225
- const label = firstEnqueueLabel(enqueue);
226
- expect(label).toContain("removed");
227
- expect(label).toContain("❤️");
228
- });
229
-
230
- it("skips when reactions array is empty", async () => {
231
- const { handler, enqueue } = createReactionTestHarness();
232
- await invokeReactionEvent(
233
- handler,
234
- {
235
- reactionsAdded: [],
236
- from: { id: "u1", aadObjectId: "allowed-aad", name: "User" },
237
- replyToId: "msg-3",
238
- },
239
- "added",
240
- );
241
-
242
- expect(enqueue).not.toHaveBeenCalled();
243
- });
244
-
245
- it("skips when from.id is missing", async () => {
246
- const { handler, enqueue } = createReactionTestHarness();
247
- await invokeReactionEvent(
248
- handler,
249
- {
250
- reactionsAdded: [{ type: "like" }],
251
- from: {},
252
- replyToId: "msg-4",
253
- },
254
- "added",
255
- );
256
-
257
- expect(enqueue).not.toHaveBeenCalled();
258
- });
259
- });
260
-
261
- describe("sender authorization", () => {
262
- it("drops reaction from non-allowlisted DM sender", async () => {
263
- const { handler, enqueue } = createReactionTestHarness();
264
- await invokeReactionEvent(
265
- handler,
266
- {
267
- reactionsAdded: [{ type: "like" }],
268
- from: { id: "bad-user", aadObjectId: "not-allowed", name: "Attacker" },
269
- replyToId: "msg-5",
270
- },
271
- "added",
272
- );
273
-
274
- expect(enqueue).not.toHaveBeenCalled();
275
- });
276
-
277
- it("allows reaction from allowlisted DM sender", async () => {
278
- const { handler, enqueue } = createReactionTestHarness();
279
- await invokeReactionEvent(
280
- handler,
281
- {
282
- reactionsAdded: [{ type: "like" }],
283
- from: { id: "good-user", aadObjectId: "allowed-aad", name: "Alice" },
284
- replyToId: "msg-6",
285
- },
286
- "added",
287
- );
288
-
289
- expect(enqueue).toHaveBeenCalledOnce();
290
- });
291
-
292
- it("allows reaction from static access group DM sender", async () => {
293
- const mockRuntime = buildMockRuntime();
294
- setMSTeamsRuntime(mockRuntime);
295
- const cfg: KlawConfig = {
296
- accessGroups: {
297
- operators: {
298
- type: "message.senders",
299
- members: { msteams: ["allowed-aad"] },
300
- },
301
- },
302
- channels: {
303
- msteams: {
304
- dmPolicy: "allowlist",
305
- allowFrom: ["accessGroup:operators"],
306
- },
307
- },
308
- } as KlawConfig;
309
- const handler = createMSTeamsReactionHandler(buildDeps(cfg, mockRuntime));
310
- const enqueue = mockRuntime.system.enqueueSystemEvent as ReturnType<typeof vi.fn>;
311
-
312
- await invokeReactionEvent(
313
- handler,
314
- {
315
- reactionsAdded: [{ type: "like" }],
316
- from: { id: "good-user", aadObjectId: "allowed-aad", name: "Alice" },
317
- replyToId: "msg-7",
318
- },
319
- "added",
320
- );
321
-
322
- expect(enqueue).toHaveBeenCalledOnce();
323
- });
324
- });
325
- });
@@ -1,122 +0,0 @@
1
- import { normalizeMSTeamsConversationId } from "../inbound.js";
2
- import type { MSTeamsMessageHandlerDeps } from "../monitor-handler.types.js";
3
- import { getMSTeamsRuntime } from "../runtime.js";
4
- import type { MSTeamsTurnContext } from "../sdk-types.js";
5
- import { resolveMSTeamsSenderAccess } from "./access.js";
6
-
7
- /** Teams reaction type names → Unicode emoji. */
8
- const TEAMS_REACTION_EMOJI: Record<string, string> = {
9
- like: "👍",
10
- heart: "❤️",
11
- laugh: "😆",
12
- surprised: "😮",
13
- sad: "😢",
14
- angry: "😡",
15
- };
16
-
17
- /**
18
- * Map a Teams reaction type string to a Unicode emoji.
19
- * Falls back to the raw type if not recognized.
20
- */
21
- function mapReactionEmoji(reactionType: string): string {
22
- return TEAMS_REACTION_EMOJI[reactionType] ?? reactionType;
23
- }
24
-
25
- type ReactionDirection = "added" | "removed";
26
-
27
- /**
28
- * Create a handler for MS Teams reaction activities (reactionsAdded / reactionsRemoved).
29
- * The returned function accepts a turn context and a direction string.
30
- */
31
- export function createMSTeamsReactionHandler(deps: MSTeamsMessageHandlerDeps) {
32
- const { cfg, log } = deps;
33
- const core = getMSTeamsRuntime();
34
- const msteamsCfg = cfg.channels?.msteams;
35
-
36
- return async function handleReaction(
37
- context: MSTeamsTurnContext,
38
- direction: ReactionDirection,
39
- ): Promise<void> {
40
- const activity = context.activity;
41
-
42
- // Reactions are carried in reactionsAdded / reactionsRemoved on the activity.
43
- const reactions: Array<{ type?: string }> =
44
- direction === "added"
45
- ? ((activity as unknown as { reactionsAdded?: Array<{ type?: string }> }).reactionsAdded ??
46
- [])
47
- : ((activity as unknown as { reactionsRemoved?: Array<{ type?: string }> })
48
- .reactionsRemoved ?? []);
49
-
50
- if (reactions.length === 0) {
51
- log.debug?.("reaction activity has no reactions; skipping");
52
- return;
53
- }
54
-
55
- const from = activity.from;
56
- if (!from?.id) {
57
- log.debug?.("reaction activity missing from.id; skipping");
58
- return;
59
- }
60
-
61
- const rawConversationId = activity.conversation?.id ?? "";
62
- const conversationId = normalizeMSTeamsConversationId(rawConversationId);
63
- const conversationType = activity.conversation?.conversationType ?? "personal";
64
- const isGroupChat = conversationType === "groupChat" || activity.conversation?.isGroup === true;
65
- const isChannel = conversationType === "channel";
66
- const isDirectMessage = !isGroupChat && !isChannel;
67
-
68
- const senderId = from.aadObjectId ?? from.id;
69
- const senderName = from.name ?? from.id;
70
-
71
- if (msteamsCfg) {
72
- const senderAccess = await resolveMSTeamsSenderAccess({ cfg, activity });
73
- if (senderAccess.senderAccess.decision !== "allow") {
74
- log.debug?.("dropping reaction (access denied)", {
75
- sender: senderId,
76
- reason: senderAccess.senderAccess.reasonCode,
77
- });
78
- return;
79
- }
80
- }
81
-
82
- // Resolve the agent route for this conversation/sender.
83
- // Extract teamId for team-scoped routing bindings (channel/group reactions).
84
- const teamId = isDirectMessage
85
- ? undefined
86
- : (activity as unknown as { channelData?: { team?: { id?: string } } }).channelData?.team?.id;
87
- const route = core.channel.routing.resolveAgentRoute({
88
- cfg,
89
- channel: "msteams",
90
- peer: {
91
- kind: isDirectMessage ? "direct" : isChannel ? "channel" : "group",
92
- id: isDirectMessage ? senderId : conversationId,
93
- },
94
- ...(teamId ? { teamId } : {}),
95
- });
96
-
97
- // The replyToId points to the message that was reacted to.
98
- const targetMessageId = (activity as unknown as { replyToId?: string }).replyToId ?? "unknown";
99
-
100
- for (const reaction of reactions) {
101
- const reactionType = reaction.type ?? "unknown";
102
- const emoji = mapReactionEmoji(reactionType);
103
- const label =
104
- direction === "added"
105
- ? `Teams reaction ${emoji} added by ${senderName} on message ${targetMessageId}`
106
- : `Teams reaction ${emoji} removed by ${senderName} from message ${targetMessageId}`;
107
-
108
- log.info(`reaction ${direction}`, {
109
- sender: senderId,
110
- reactionType,
111
- emoji,
112
- targetMessageId,
113
- conversationId,
114
- });
115
-
116
- core.system.enqueueSystemEvent(label, {
117
- sessionKey: route.sessionKey,
118
- contextKey: `msteams:reaction:${conversationId}:${targetMessageId}:${senderId}:${reactionType}:${direction}`,
119
- });
120
- }
121
- };
122
- }
@@ -1,30 +0,0 @@
1
- import { resolveThreadSessionKeys } from "klaw/plugin-sdk/routing";
2
-
3
- // Strip any trailing `:thread:<id>` segments from a session key. Thread ids are
4
- // timestamps/uuids and never contain `:`, so the segment boundary is unambiguous;
5
- // the `+` covers pathological keys with multiple compounded suffixes.
6
- const TRAILING_THREAD_SUFFIX = /(?::thread:[^:]+)+$/;
7
-
8
- export function resolveMSTeamsRouteSessionKey(params: {
9
- baseSessionKey: string;
10
- isChannel: boolean;
11
- conversationMessageId?: string;
12
- replyToId?: string;
13
- }): string {
14
- const channelThreadId = params.isChannel
15
- ? (params.conversationMessageId ?? params.replyToId ?? undefined)
16
- : undefined;
17
- // Re-derive from a clean base. If a caller hands us a session key that is
18
- // already thread-qualified (e.g. a `route.sessionKey` mutated in place by a
19
- // prior turn whose object is still held in the resolved-route cache, see
20
- // src/routing/resolve-route.ts cache-miss return), naively appending the
21
- // current thread id would compound into `…:thread:OLD:thread:NEW` and route
22
- // the turn to a malformed lane that splits same-thread context across turns.
23
- // Stripping makes this helper idempotent regardless of caller hygiene. (#66771)
24
- const cleanBase = params.baseSessionKey.replace(TRAILING_THREAD_SUFFIX, "");
25
- return resolveThreadSessionKeys({
26
- baseSessionKey: cleanBase,
27
- threadId: channelThreadId,
28
- parentSessionKey: channelThreadId ? cleanBase : undefined,
29
- }).sessionKey;
30
- }
@@ -1,158 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import type { KlawConfig, RuntimeEnv } from "../runtime-api.js";
3
- import type { MSTeamsConversationStore } from "./conversation-store.js";
4
- import {
5
- type MSTeamsActivityHandler,
6
- type MSTeamsMessageHandlerDeps,
7
- registerMSTeamsHandlers,
8
- } from "./monitor-handler.js";
9
- import { installMSTeamsTestRuntime } from "./monitor-handler.test-helpers.js";
10
- import type { MSTeamsTurnContext } from "./sdk-types.js";
11
-
12
- const runtimeApiMockState = vi.hoisted(() => ({
13
- dispatchReplyFromConfigWithSettledDispatcher: vi.fn(async (params: { ctxPayload: unknown }) => ({
14
- queuedFinal: false,
15
- counts: {},
16
- capturedCtxPayload: params.ctxPayload,
17
- })),
18
- }));
19
-
20
- vi.mock("klaw/plugin-sdk/inbound-reply-dispatch", () => {
21
- return {
22
- dispatchReplyFromConfigWithSettledDispatcher:
23
- runtimeApiMockState.dispatchReplyFromConfigWithSettledDispatcher,
24
- };
25
- });
26
-
27
- vi.mock("./reply-dispatcher.js", () => ({
28
- createMSTeamsReplyDispatcher: () => ({
29
- dispatcher: {},
30
- replyOptions: {},
31
- markDispatchIdle: vi.fn(),
32
- }),
33
- }));
34
-
35
- function createDeps(): MSTeamsMessageHandlerDeps {
36
- installMSTeamsTestRuntime();
37
-
38
- return {
39
- cfg: {} as KlawConfig,
40
- runtime: { error: vi.fn() } as unknown as RuntimeEnv,
41
- appId: "test-app",
42
- adapter: {} as MSTeamsMessageHandlerDeps["adapter"],
43
- tokenProvider: {
44
- getAccessToken: vi.fn(async () => "token"),
45
- },
46
- textLimit: 4000,
47
- mediaMaxBytes: 1024 * 1024,
48
- conversationStore: {
49
- get: vi.fn(async () => null),
50
- upsert: vi.fn(async () => undefined),
51
- list: vi.fn(async () => []),
52
- remove: vi.fn(async () => false),
53
- findPreferredDmByUserId: vi.fn(async () => null),
54
- findByUserId: vi.fn(async () => null),
55
- } satisfies MSTeamsConversationStore,
56
- pollStore: {
57
- recordVote: vi.fn(async () => null),
58
- } as unknown as MSTeamsMessageHandlerDeps["pollStore"],
59
- log: {
60
- info: vi.fn(),
61
- debug: vi.fn(),
62
- error: vi.fn(),
63
- } as unknown as MSTeamsMessageHandlerDeps["log"],
64
- };
65
- }
66
-
67
- function createActivityHandler() {
68
- const messageHandlers: Array<(context: unknown, next: () => Promise<void>) => Promise<void>> = [];
69
- const run = vi.fn(async (context: unknown) => {
70
- const activityType = (context as MSTeamsTurnContext).activity?.type;
71
- if (activityType !== "message") {
72
- return;
73
- }
74
- for (const handler of messageHandlers) {
75
- await handler(context, async () => {});
76
- }
77
- });
78
-
79
- let handler: MSTeamsActivityHandler & {
80
- run: NonNullable<MSTeamsActivityHandler["run"]>;
81
- };
82
- handler = {
83
- onMessage: (nextHandler) => {
84
- messageHandlers.push(nextHandler);
85
- return handler;
86
- },
87
- onMembersAdded: () => handler,
88
- onReactionsAdded: () => handler,
89
- onReactionsRemoved: () => handler,
90
- run,
91
- };
92
-
93
- return { handler, run };
94
- }
95
-
96
- describe("msteams adaptive card action invoke", () => {
97
- beforeEach(() => {
98
- runtimeApiMockState.dispatchReplyFromConfigWithSettledDispatcher.mockClear();
99
- });
100
-
101
- it("forwards adaptive card invoke values to the agent as message text", async () => {
102
- const deps = createDeps();
103
- const { handler, run } = createActivityHandler();
104
- const registered = registerMSTeamsHandlers(handler, deps) as MSTeamsActivityHandler & {
105
- run: NonNullable<MSTeamsActivityHandler["run"]>;
106
- };
107
- const payload = {
108
- action: {
109
- type: "Action.Submit",
110
- data: {
111
- intent: "deploy",
112
- environment: "prod",
113
- },
114
- },
115
- trigger: "button-click",
116
- };
117
-
118
- await registered.run({
119
- activity: {
120
- id: "invoke-1",
121
- type: "invoke",
122
- name: "adaptiveCard/action",
123
- channelId: "msteams",
124
- serviceUrl: "https://service.example.test",
125
- from: {
126
- id: "user-bf",
127
- aadObjectId: "user-aad",
128
- name: "User",
129
- },
130
- recipient: {
131
- id: "bot-id",
132
- name: "Bot",
133
- },
134
- conversation: {
135
- id: "19:personal-chat;messageid=abc123",
136
- conversationType: "personal",
137
- },
138
- channelData: {},
139
- attachments: [],
140
- value: payload,
141
- },
142
- sendActivity: vi.fn(async () => ({ id: "activity-id" })),
143
- sendActivities: async () => [],
144
- } as unknown as MSTeamsTurnContext);
145
-
146
- expect(run).not.toHaveBeenCalled();
147
- expect(runtimeApiMockState.dispatchReplyFromConfigWithSettledDispatcher).toHaveBeenCalledTimes(
148
- 1,
149
- );
150
- const dispatched = runtimeApiMockState.dispatchReplyFromConfigWithSettledDispatcher.mock
151
- .calls[0]?.[0] as { ctxPayload?: Record<string, unknown> } | undefined;
152
- expect(dispatched?.ctxPayload?.RawBody).toBe(JSON.stringify(payload));
153
- expect(dispatched?.ctxPayload?.BodyForAgent).toBe(JSON.stringify(payload));
154
- expect(dispatched?.ctxPayload?.CommandBody).toBe(JSON.stringify(payload));
155
- expect(dispatched?.ctxPayload?.SessionKey).toBe("msteams:direct:user-aad");
156
- expect(dispatched?.ctxPayload?.SenderId).toBe("user-aad");
157
- });
158
- });